EOF 总是负数吗?

发布于 2024-08-08 18:03:17 字数 121 浏览 7 评论 0原文

EOF 总是负数吗?

我正在考虑编写一个函数,该函数读取输入中的下一个单词,并返回在其中找到该单词的行号,如果已到达输入末尾,则返回 EOF 。如果 EOF 不一定为负,则该函数将不正确。

Is EOF always negative?

I'm thinking of writing a function that reads the next word in the input and returns the line number the word was found in or EOF if the end of the input has been reached. If EOF is not necessarily negative, the function would be incorrect.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

微凉徒眸意 2024-08-15 18:03:17

EOF 始终为 == EOF。不要假设任何其他事情。

在第二次阅读标准时(以及根据这里的其他一些评论),似乎 EOF 始终为负 - 并且对于此问题中指定的用途(行号或 EOF )它会起作用。我想要警告(并且仍然这样做)的是假设字符为正,而 EOF 为负。

请记住,符合标准的 C 实现可能具有负字符值 - 这甚至在“C 编程语言”(K&R) 中提到过。打印字符始终为正,但在某些架构(可能都是古代)上,控制字符为负。 C 标准没有指定 char 类型是有符号还是无符号,唯一保证跨平台具有相同值的字符常量是 '\0'

EOF is always == EOF. Don't assume anything else.

On a second reading of the standard (and as per some other comments here) it seems EOF is always negative - and for the use specified in this question (line number or EOF) it would work. What I meant to warn against (and still do) is assuming characters are positive and EOF is negative.

Remember that it's possible for a standard conforming C implementation to have negative character values - this is even mentioned in 'The C programming language' (K&R). Printing characters are always positive, but on some architectures (probably all ancient), control characters are negative. The C standard does not specify whether the char type is signed or unsigned, and the only character constant guaranteed to have the same value across platforms, is '\0'.

可是我不能没有你 2024-08-15 18:03:17

是的,EOF 总是负数。

标准说:

7.19 输入/输出
7.19.1 简介

3 宏是 [...] EOF
展开为整数常量
表达式,类型为 int 和 a
负值,返回值
几个函数来表示
文件结尾,即不再输入
来自流;

请注意,对“plain”char 进行签名没有问题。处理 char 函数,专门将字符转换为 unsigned char,然后转换为 int< /code>,以便所有有效字符都具有正值。例如:

int fgetc(FILE *stream)

7.19.7.1
... fgetc 函数获取该字符作为转换为 int 的 unsigned char ...

Yes, EOF is always negative.

The Standard says:

7.19 Input/output
7.19.1 Introduction

3 The macros are [...] EOF which
expands to an integer constant
expression, with type int and a
negative value, that is returned by
several functions to indicate
end-of-file, that is, no more input
from a stream;

Note that there's no problem with "plain" char being signed. The <stdio.h> functions which deal with chars, specifically cast the characters to unsigned char and then to int, so that all valid characters have a positive value. For example:

int fgetc(FILE *stream)

7.19.7.1
... the fgetc function obtains that character as an unsigned char converted to an int ...

旧时光的容颜 2024-08-15 18:03:17

让该函数返回

  • 找到单词的行号,
  • 如果已到达输入末尾,则返回 -1

问题已解决,无需依赖任何 EOF 值。调用者可以轻松测试是否大于或等于 0 是否成功调用,否则假设 EOF/IO 错误。

Have that function return

  • the line number the word was found in
  • or -1 in case the end of the input has been reached

Problem solved, without a need for relying on any EOF values. The caller can easily test for greater-or-equal-to-zero for a successful call, and assume EOF/IO-error otherwise.

盗梦空间 2024-08-15 18:03:17

来自在线草案 n1256,17.9。 1.3:

EOF

它扩展为一个整数常量表达式,具有 int 类型和负值,
由多个函数返回以指示文件结束,即不再有输入
来自流;

EOF 始终为负数,但可能并不总是 -1。

对于此类问题,我更喜欢通过返回错误代码(SUCCESSEND_OF_FILEREAD_ERROR 等)作为函数的返回值,然后将感兴趣的数据写入单独的参数,例如

int getNextWord (FILE *stream, char *buffer, size_t bufferSize, int *lineNumber)
{
  if (!fgets(buffer, bufferSize, stream))
  {
    if (feof(stream)) return END_OF_FILE; else return READ_ERROR;
  }
  else
  {
    // figure out the line number
    *lineNumber = ...;
  }
  return SUCCESS;
}      

From the online draft n1256, 17.9.1.3:

EOF

which expands to an integer constant expression, with type int and a negative value,
that is returned by several functions to indicate end-of-file, that is, no more input
from a stream;

EOF is always negative, though it may not always be -1.

For issues like this, I prefer separating error conditions from data by returning an error code (SUCCESS, END_OF_FILE, READ_ERROR, etc.) as the function's return value, and then writing the data of interest to separate parameters, such as

int getNextWord (FILE *stream, char *buffer, size_t bufferSize, int *lineNumber)
{
  if (!fgets(buffer, bufferSize, stream))
  {
    if (feof(stream)) return END_OF_FILE; else return READ_ERROR;
  }
  else
  {
    // figure out the line number
    *lineNumber = ...;
  }
  return SUCCESS;
}      
梦在深巷 2024-08-15 18:03:17

EOF 是一个条件,而不是一个值。该标记的确切值是实现定义的。在很多情况下,它是一个负数。

EOF is a condition, rather than a value. The exact value of this sentinel is implementation defined. In a lot of cases, it is a negative number.

落花浅忆 2024-08-15 18:03:17

来自维基百科

EOF 的实际值为
系统相关的负数,
通常为-1,保证为
不等于任何有效的字符代码。

但没有参考...

来自安全编码:检测并处理输入输出错误
EOF 为负数,但仅当 sizeof(int) > sizeof(char)

From wikipedia :

The actual value of EOF is a
system-dependent negative number,
commonly -1, which is guaranteed to be
unequal to any valid character code.

But no references ...

From Secure Coding : Detect and handle input and output errors
EOF is negative but only when sizeof(int) > sizeof(char).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文