/* 在磁盤建立文件,而後接受鍵盤輸入。 * 從鍵盤輸入完之後,把文件輸出到磁盤並保存 * 最後把磁盤內容打印到屏幕*/ #include <stdio.h> #include <stdlib.h> int main(void) { FILE *fp; char ch; puts("Input your data, and enter Ctrl+Z to exit:\n"); /*check if success open the file*/ if((fp = fopen("my data file.txt", "w")) == NULL){ fprintf(stderr, "Error opening %s.\n", fp); exit(1); } /*Get a character from keyboard*/ while ((ch = getchar()) != EOF) /*Write a character to the file*/ putc(ch,fp); /*remenber close the file*/ fclose(fp); puts("You data file is:\n"); /*Ropen the file ready to read*/ if((fp = fopen("my data file.txt", "r")) == NULL){ fprintf(stderr, "Error opening %s.\n", fp); exit(1); } ch = fgetc(fp); while(ch != EOF){ putchar(ch); ch = fgetc(fp); } ///*read a character from file*/ //while((ch=getc(fp)) != EOF) // /*display a character on screen*/ // printf("%c",ch); fclose(fp); printf("\n"); return 0; }