GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::fwrite - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. cstdio
  3. fwrite

最終更新日時: 2026年09月19日 21時31分51秒 (JST)
rotarymars が更新

履歴 編集

function
<cstdio>

std::fwrite

namespace std {
  size_t fwrite(const void *buffer, size_t size, size_t count, FILE *stream);
}

概要

ストリームにcount個のsizeバイトの要素を書き込む。

ストリーム内の位置は、書き込まれたバイトだけ進む。

書き込み時にエラーが発生した場合、ストリーム内の位置は不定である。

また、書き込みには内部的にfputcを使用する。

要件

  • bufferは有効なポインタであること。
  • streamは有効なファイルストリームであること。
  • bufferが指すメモリ領域とstreamが指すファイルストリームの内部バッファが重複していないこと。

戻り値

正常に書き込むことのできた要素の数を返す。

効果

bufferが指すメモリからcount個のsizeバイトの要素をstreamに書き込む。

書き込みエラーが生じなければ、書き込むデータの大きさはcount * sizeバイトである。

例

#include <cstdio>

int main() {
  std::FILE *file = std::fopen("output.txt", "w");
  if (!file) {
    std::perror("ファイルを開けませんでした");
    return 1;
  }

  const char data[] = "Hello, World!\n";
  /*
  厳密には、sizeof(char)は1バイトであることが保証されているため、
  sizeof(data) - 1は、文字列の長さ(ヌル終端文字を除く)と等しくなる。
  */
  std::size_t count = std::fwrite(data, sizeof(char), sizeof(data) - 1, file);
  std::printf("書き込んだデータの長さ: %zu\n", count);

  std::fclose(file);
  return 0;
}

出力例

書き込んだデータの長さ: 14

ファイル出力(output.txt)

Hello, World!

処理系

  • Clang: 9.0.1 ✅
  • GCC: ??
  • Visual C++: ??
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.