I need to write something to a txt file and read the contents, and then print them on the screen. Below is the code I wrote, it can correctly create and write contents to a file, but it cannot read from a file and print correctly.
#include<stdio.h> #include<stdlib.h> main() { char filename[20]={"c:\\test.txt"}; FILE *inFile; char c; inFile=fopen(filename,"w+"); if(inFile==NULL) { printf("An error occoured!"); exit(1); } while((c=getchar())!=EOF) fputc(c,inFile); fputc('\0',inFile); while((c=fgetc(inFile))!=EOF) putchar(c); }
Someone will tell me what happened to this program, especially the last two lines. Thank you in advance.
source share