20131014【C言語】fwriteでファイルに書き出す
お題
fwirteでファイルに書き出す
ソース
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp1, *fp2;
int count = 0;
double d;
char ch, uch;
/* Open values file */
if *1 == NULL) {
fprintf(stderr, "Error while opening values file.\n");
exit(1);
}
/* Input double value */
do {
printf("Continue to input? y/n:\n");
ch = getc(stdin);
uch = toupper(ch);
if (uch == 'Y') {
printf("Please input double number: ");
scanf("%lf", &d);
if (fwrite(&d, sizeof(double), 1, fp1) != 1) {
fprintf(stderr, "Error while writing to values file.\n");
exit(1);
}
count++;
}
} while (uch != 'N');
/* Close values file */
if (fclose(fp1) == EOF) {
fprintf(stderr, "Error while closing values file.\n");
exit(1);
}
/* Open count file */
if *2 == NULL) {
fprintf(stderr, "Error while opening count file.\n");
exit(1);
}
/* Write count to count file */
if (fwrite(&count, sizeof(int), count, fp2) != 1) {
fprintf(stderr, "Error while writing to count file.\n");
exit(1);
}
/* Close count file */
if (fclose(fp2) == EOF) {
fprintf(stderr, "Error closing count file.\n");
exit(1);
}
return 0;
}
実行結果
省略