我有一个名为myfile.txt的文本文件,该文件读取:
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
每当遇到a n时,ftell都会跳过一个值,例如它跳过了值7。为什么这样呢?请详细解释我,我想知道。
问题在于newline字符,在windows中是 rn
(Windows Carriagar返回 r n是否由两个字符或一个字符组成?)。
尝试更改这些:
fopen("myfile.txt","r");
这些:
fopen("myfile.txt","rb");
b
用于二进制模式。
二进制模式在窗口上有所作为,其中文本模式将两个字符运输返回映射到单个新线字符。注意:Linux上不需要映射。