白spaces在txt文件中取space吗?
Do white spaces take space in txt files?
我想知道白色 space 是否在 txt
文件中为 C 取 space。例如,如果我要使用 [= 取回文件指针17=] 或者如果我要 fscanf
来自文件的输入,我需要考虑白色 space 吗? A
假设一个文件有这一行:
michael 100 20 simon 200 30 daniel 300 100
如果我想循环扫描文件中的第一个数据,我应该怎么做
fscanf(fp,"%s%d%d",structure[i].name,&structure[i].number1,&structure[i].number2);
或
fscanf(fp,"%s %d %d ",structure[i].name,&structure[i].number1,&structure[i].number2);
此外,我在取回文件指针时是否需要考虑这些space?例如获取文件中的第二个人
fseek(fp,-sizeof(structure[i-1]),SEEK_CUR)
或
fseek(fp,-(sizeof(structure[i-1])+3),SEEK_CUR)//for there are 3 white spaces for each person.
关于你的第一个问题:不,白色 space 不需要在 fscanf()
中考虑。
关于你的第二个问题:这些都不起作用。您似乎误解了信息在计算机中的存储方式。在程序中,一个整数是32位(4字节),所以sizeof(int)
总是return4。在一个文件中,每个字符是一个字节,所以“314”占用3个字节,“314159” " 需要 6。因此,通常不建议混搭 fscanf
和 fseek
/fgetc
。
我想知道白色 space 是否在 txt
文件中为 C 取 space。例如,如果我要使用 [= 取回文件指针17=] 或者如果我要 fscanf
来自文件的输入,我需要考虑白色 space 吗? A
假设一个文件有这一行:
michael 100 20 simon 200 30 daniel 300 100
如果我想循环扫描文件中的第一个数据,我应该怎么做
fscanf(fp,"%s%d%d",structure[i].name,&structure[i].number1,&structure[i].number2);
或
fscanf(fp,"%s %d %d ",structure[i].name,&structure[i].number1,&structure[i].number2);
此外,我在取回文件指针时是否需要考虑这些space?例如获取文件中的第二个人
fseek(fp,-sizeof(structure[i-1]),SEEK_CUR)
或
fseek(fp,-(sizeof(structure[i-1])+3),SEEK_CUR)//for there are 3 white spaces for each person.
关于你的第一个问题:不,白色 space 不需要在 fscanf()
中考虑。
关于你的第二个问题:这些都不起作用。您似乎误解了信息在计算机中的存储方式。在程序中,一个整数是32位(4字节),所以sizeof(int)
总是return4。在一个文件中,每个字符是一个字节,所以“314”占用3个字节,“314159” " 需要 6。因此,通常不建议混搭 fscanf
和 fseek
/fgetc
。