Skip to content

Instantly share code, notes, and snippets.

@hiroshiro
Created March 18, 2015 10:34
Show Gist options
  • Save hiroshiro/b9f93368210238c20f49 to your computer and use it in GitHub Desktop.
Save hiroshiro/b9f93368210238c20f49 to your computer and use it in GitHub Desktop.
'0'から'9'までの10文字をファイル(WriteData.txt)に書き込む。
/* WriteFile01.c */
/* '0'から'9'までの10文字をファイル(WriteData.txt)に書き込む。 */
#include <stdio.h>
int main(void)
{
FILE *pFile = NULL; // ファイルポインタを定義
char *pszInFileName = "WriteData.txt"; // 書き込みファイル名
char *writeData = "0123456789"; // 書き込むデータとしての文字列
size_t size_tDataSize = sizeof(char); // 書き込むデータの長さ
size_t size_tDataCount = 10; // 書き込むデータの個数
size_t size_tWriteCount = 0; // 書き込み結果のデータの個数
int iRet = 0;
pFile = fopen("pszInFileName", "w");
if (pFile == NULL) {
printf("fopenがNULLをリターン。\n");
return -1;
}
// ファイルへのデータ書き込み
size_tWriteCount = fwrite(writeData, size_tDataSize, size_tDataCount, pFile);
if (size_tWriteCount != size_tDataCount) {
printf("fwriteでエラー発生。\n");
return -1;
}
iRet = fclose(pFile); // ファイルのクローズ
printf("ファイルのクローズの戻り値: %d\n", iRet);
return 0;
}
/* 実行結果 */
/* ファイルのクローズの戻り値: 0 */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment