/* Program "exercise03-1.c" --- ファイルへの出力 */ #include #include int main(void) { char name[256]; int age; char hometown[256]; char comment[256]; FILE *fp; /* ファイルオープン */ if((fp = fopen("myprofile.dat", "w")) == NULL ) { printf(" *** file open error *** \n"); exit(1); } /* 名前の入力 */ printf("input name>"); scanf("%s", name); /* 年齢の入力 */ printf("input age>"); scanf("%d", &age); /* 出身地の入力 */ printf("input hometown>"); scanf("%s", hometown); /* その他コメントの入力 */ printf("input comment>"); scanf("%s", comment); /* 入力されたデータの表示 */ printf("name=%s\nage=%d\nhometown=%s\ncomment=%s\n", name, age, hometown, comment); /* 入力されたデータのファイル出力*/ fprintf(fp,"name=%s\nage=%d\nhometown=%s\ncomment=%s\n", name, age, hometown, comment); /* ファイルクローズ */ fclose( fp ); return 0; }