我有一个文本文件,其内容如下:
写什么
我首先是这样创建此文件的:
FILE * fPointer;
fPointer = fopen("fileName.txt" , "w"); //create the file
fprintf(fPointer,"what to write\n"); //write to the file
我这样读取文件内容:
fPointer = fopen("fileName.txt" , "r"); //too read the file
char singleLine [100];
while(!feof(fPointer)){ //while file is not finished
fgets(singleLine,100,fPointer);
puts(singleLine);
}
我希望只获取一次文件的“写入内容”内容,但这是结果:
如您所见,由于某种原因,我两次获得文件内容,所以为什么会有这种想法?
我已检查文件位置正确且文件存在。
我也检查了this answer,但是我对getc
方法并不熟悉,我想知道自己在做错什么。