//numberfile.c -- 演示fprintf()函數的用法 #include <stdio.h> #include <stdlib.h> void clear_kb(void); int main(void){ FILE *fp; float data[5]; int count; char filename[20]; puts("Enter 5 floating-point numerical values:"); for(count = 0; count < 5; count++) scanf("%f", &data[count]); // 得到文件名,並打開文件。首先要清除stdin中的額外字符 clear_kb(); puts("Enter a name for the file:"); gets(filename); if((fp = fopen(filename, "w")) == NULL){ fprintf(stderr, "Error opening file %s.", filename); exit(1); } for(count = 0; count < 5; count++){ //將數值輸出到磁盤文件 fprintf(fp, "\ndata[%d] = %f", count, data[count]); //將數值輸出到流 fprintf(stdout, "\ndata[%d] = %f", count, data[count]); } fclose(fp); printf("\n"); return 0; } // 清理stdin中的其餘字符 void clear_kb(void){ char junk[80]; gets(junk); }