我有一個名爲myfile.txt的文本文件,其內容爲:爲什麼ftell會跳過文件中的某些位置?
line 1
l
我的代碼:
#include<stdio.h>
int main(){
FILE *f = fopen("myfile.txt","r");
if(f==NULL){
FILE *fp=fopen("myfile.txt","w");
fclose(fp);
f = fopen("myfile.txt","r");
}
while(!feof(f)){
printf("\ncharacter number %d ",ftell(f));
putchar(fgetc(f));
}
fclose(f);
return 0;
}
輸出是:
character number 0 l
character number 1 i
character number 2 n
character number 3 e
character number 4
character number 5 1
character number 6
character number 8 l
character number 9
只要遇到\ n時, ftell跳過一個值,例如跳過了值7.爲什麼這樣呢?請詳細解釋我,我想知道。
請參閱相關問題https://stackoverflow.com/questions/10651108/why-does-ftell-shows-wrong-position-after-fread?rq=1 – Saustin
您是否正在使用Windows? –
@AjayBrahmakshatriya是的,我正在使用Windows。 –