#include <stdio.h>
#include <stdlib.h>
int main()
{
int count, total, i1;
long li[5], lo[5];
FILE *stream;
/*
データの設定
*/
for (i1 = 0; i1 < 5; i1++)
lo[i1] = i1 + 1;
/*
ファイルのオープン(出力)
*/
if((stream = fopen("data", "w")) == NULL)
exit(1);
/*
ファイルへのデータの出力とファイルのクローズ
*/
count = fwrite(lo, sizeof(long), 5, stream);
printf("%d 個のデータを出力しました\n", count);
fclose(stream);
/*
ファイルのオープン(入力)
*/
if((stream = fopen("data", "r")) == NULL)
exit(1);
/*
データの読込
*/
total = 0;
i1 = 0;
while (!feof(stream)) {
count = fread(&(li[i1]), sizeof(long), 1, stream);
if (ferror(stream)) {
perror("***入力エラー***");
exit(1);
}
total += count;
i1++;
}
printf("%d 個のデータを入力しました\n", total);
for (i1 = 0; i1 < 5; i1++)
printf("%ld ", li[i1]);
printf("\n");
return 0;
}