gpt4 book ai didi

c - fgetpos() 行为取决于换行符

转载 作者:可可西里 更新时间:2023-11-01 13:11:41 25 4
gpt4 key购买 nike

考虑这两个文件:

file1.txt(Windows 换行符)

abc\r\n
def\r\n

file2.txt(Unix 换行符)

abc\n
def\n

我注意到对于 file2.txt,使用 fgetpos 获得的位置没有正确递增。我在 Windows 上工作。

让我举个例子。以下代码:

#include<cstdio>

void read(FILE *file)
{
int c = fgetc(file);
printf("%c (%d)\n", (char)c, c);

fpos_t pos;
fgetpos(file, &pos); // save the position
c = fgetc(file);
printf("%c (%d)\n", (char)c, c);

fsetpos(file, &pos); // restore the position - should point to previous
c = fgetc(file); // character, which is not the case for file2.txt
printf("%c (%d)\n", (char)c, c);
c = fgetc(file);
printf("%c (%d)\n", (char)c, c);
}

int main()
{
FILE *file = fopen("file1.txt", "r");
printf("file1:\n");
read(file);
fclose(file);

file = fopen("file2.txt", "r");
printf("\n\nfile2:\n");
read(file);
fclose(file);

return 0;
}

给出这样的结果:

file1:
a (97)
b (98)
b (98)
c (99)


file2:
a (97)
b (98)
  (-1)
  (-1)

file1.txt 按预期工作,而 file2.txt 表现异常。为了解释它有什么问题,我尝试了以下代码:

void read(FILE *file)
{
int c;
fpos_t pos;
while (1)
{
fgetpos(file, &pos);
printf("pos: %d ", (int)pos);
c = fgetc(file);
if (c == EOF) break;
printf("c: %c (%d)\n", (char)c, c);
}
}

int main()
{
FILE *file = fopen("file1.txt", "r");
printf("file1:\n");
read(file);
fclose(file);

file = fopen("file2.txt", "r");
printf("\n\nfile2:\n");
read(file);
fclose(file);

return 0;
}

我得到了这个输出:

file1:
pos: 0 c: a (97)
pos: 1 c: b (98)
pos: 2 c: c (99)
pos: 3 c:
(10)
pos: 5 c: d (100)
pos: 6 c: e (101)
pos: 7 c: f (102)
pos: 8 c:
(10)
pos: 10

file2:
pos: 0 c: a (97) // something is going wrong here...
pos: -1 c: b (98)
pos: 0 c: c (99)
pos: 1 c:
(10)
pos: 3 c: d (100)
pos: 4 c: e (101)
pos: 5 c: f (102)
pos: 6 c:
(10)
pos: 8

我知道 fpos_t 不应该由编码人员解释,因为它取决于实现。但是,上面的示例解释了 fgetpos/fsetpos 的问题。

换行序列怎么可能影响文件的内部位置,甚至在它遇到那些字符之前?

最佳答案

我会说问题可能是由第二个文件混淆实现引起的,因为它是以文本模式打开的,但它不符合要求。

在标准中,

A text stream is an ordered sequence of characters composed into lines, each line consisting of zero or more characters plus a terminating new-line character

您的第二个文件流不包含有效的换行符(因为它寻找 \r\n 以在内部转换为换行符)。结果,实现可能无法正确理解行的长度,并且当您尝试在其中移动时会感到无可救药的困惑。

此外,

Characters may have to be added, altered, or deleted on input and output to conform to differing conventions for representing text in the host environment.

请记住,当您调用 fgetc 时,库不会只从文件中读取每个字节 - 它会将整个文件(这么小的文件)读取到流的缓冲区中并对其进行操作.

关于c - fgetpos() 行为取决于换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15649089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com