EOF可以和\n在同一行吗
Can EOF be on the same line as \n
正如标题所说,EOF
可以和"\n"
在同一行吗?我目前正在做一个用 C 重新创建 getline
的项目,我正在实施 EOF
发生的事情,我试图弄清楚我是否会得到文本文件的最后一行:
"blahblahblahblah\n[=10=]"
或
"blahblahblah\n
[=11=]"
EOF 是通知没有更多输入的信号。最后称为 EOF 的文件中没有实际数据。 EOF 与空字符不同 [=10=]
.
我想您想知道是否可以在不先获取换行符的情况下获取 EOF。答案是肯定的。如果文件末尾没有换行符,即最后一行结束时没有换行符,则有可能。您可以通过打开文本编辑器轻松生成这样的文件,键入一些文本并保存,不按 ENTER 退出。该文件末尾没有换行符。
Can EOF be on the same line as \n
有,但很少。
EOF
是常量,通常用于指示 1) 文件结束或 2) 罕见的输入错误。它通常不被编码为文本流中的字符。
来自 fgetc()
的 EOF
的 return 值和朋友可以随时出现 - 即使在 行 到期的中间出错了。
否则,否:
文件结尾不会出现在带有 '\n'
的 行 的末尾。通常,包含至少 1 个字符(并且没有 `'\n')的文件中的最后一行也将是 行。
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. Whether the last line requires a terminating new-line character is implementation-defined. C11dr §7.21.2 2
扩展@taskinoor 和@chux 的答案并查看您的示例后,我认为您可能会将 EOF
与字符串的 '[=12=]'
终止字节混淆。
EOF
是一个类似于 getc
return 的值,它告诉用户它无法从流中读取更多内容。这就是为什么你经常看到这个:
// fp is a FILE*, it may be stdin
// it may be a file on the disk or something
// else
int c;
while((c = getc(fp)) != EOF)
{
// doing something with the character
}
注意这里 c
被声明为 int
,因为 EOF
是一个 signed int
而那个
getc
return 是什么。我之前在这里发表的声明1是不正确的,chux
指出了我的错误,我想在这里引用 his/her 评论:
User chux comment on my answer
EOF
, as a negative constant, is often -1 and can fit in a char
, when char a signed char
.
It is not so much that it does not fit, it is that storing a EOF
in a char can be indistinguishable
from storing say a character with the value of 255 in a char
.
'[=12=]'
is on the other hand is the byte that is used to terminate a string in C. In
C a strings is nothing more than a sequence of characters that ends with that
byte. We use char
arrays to store strings. So EOF
and [=28=]
are different
things and have even different values.
脚注
1注意这里c
声明为int
,因为EOF
是一个signed int
并且
实际上不适合 char
,因此它永远不会成为 c 字符串的一部分。
正如标题所说,EOF
可以和"\n"
在同一行吗?我目前正在做一个用 C 重新创建 getline
的项目,我正在实施 EOF
发生的事情,我试图弄清楚我是否会得到文本文件的最后一行:
"blahblahblahblah\n[=10=]"
或
"blahblahblah\n
[=11=]"
EOF 是通知没有更多输入的信号。最后称为 EOF 的文件中没有实际数据。 EOF 与空字符不同 [=10=]
.
我想您想知道是否可以在不先获取换行符的情况下获取 EOF。答案是肯定的。如果文件末尾没有换行符,即最后一行结束时没有换行符,则有可能。您可以通过打开文本编辑器轻松生成这样的文件,键入一些文本并保存,不按 ENTER 退出。该文件末尾没有换行符。
Can EOF be on the same line as \n
有,但很少。
EOF
是常量,通常用于指示 1) 文件结束或 2) 罕见的输入错误。它通常不被编码为文本流中的字符。
来自 fgetc()
的 EOF
的 return 值和朋友可以随时出现 - 即使在 行 到期的中间出错了。
否则,否:
文件结尾不会出现在带有 '\n'
的 行 的末尾。通常,包含至少 1 个字符(并且没有 `'\n')的文件中的最后一行也将是 行。
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. Whether the last line requires a terminating new-line character is implementation-defined. C11dr §7.21.2 2
扩展@taskinoor 和@chux 的答案并查看您的示例后,我认为您可能会将 EOF
与字符串的 '[=12=]'
终止字节混淆。
EOF
是一个类似于 getc
return 的值,它告诉用户它无法从流中读取更多内容。这就是为什么你经常看到这个:
// fp is a FILE*, it may be stdin
// it may be a file on the disk or something
// else
int c;
while((c = getc(fp)) != EOF)
{
// doing something with the character
}
注意这里 c
被声明为 int
,因为 EOF
是一个 signed int
而那个
getc
return 是什么。我之前在这里发表的声明1是不正确的,chux
指出了我的错误,我想在这里引用 his/her 评论:
User chux comment on my answer
EOF
, as a negative constant, is often -1 and can fit in achar
, when char asigned char
. It is not so much that it does not fit, it is that storing aEOF
in a char can be indistinguishable from storing say a character with the value of 255 in achar
.
'[=12=]'
is on the other hand is the byte that is used to terminate a string in C. In C a strings is nothing more than a sequence of characters that ends with that byte. We usechar
arrays to store strings. SoEOF
and[=28=]
are different things and have even different values.
脚注
1注意这里c
声明为int
,因为EOF
是一个signed int
并且
实际上不适合 char
,因此它永远不会成为 c 字符串的一部分。