如何使用EOF通过在C文本文件来运行? [英] How to use EOF to run through a text file in C?

查看:101
本文介绍了如何使用EOF通过在C文本文件来运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了每行字符串的文本文件。欲增加为在文本文件中的每一行的数,但是,当它到达它显然需要停止该文件的结束。我试着做的EOF一些研究,但不能真正了解如何正确使用它。

I have a text file that has strings on each line. I want to increment a number for each line in the text file, but when it reaches the end of the file it obviously needs to stop. I've tried doing some research on EOF, but couldn't really understand how to use it properly.

我假设我需要一个while循环,但我不知道该怎么做。

I'm assuming I need a while loop, but I'm not sure how to do it.

谢谢!

推荐答案

EOF你如何检测取决于你使用读取流的内容:

How you detect EOF depends on what you're using to read the stream:

function                  result on EOF or error                    
--------                  ----------------------
fgets()                   NULL
fscanf()                  number of succesful conversions
                            less than expected
fgetc()                   EOF
fread()                   number of elements read
                            less than expected

检查输入呼吁上述相应条件的结果,然后调用的feof()来确定结果是由于打EOF或其他一些错误。

Check the result of the input call for the appropriate condition above, then call feof() to determine if the result was due to hitting EOF or some other error.

使用与fgets()

 char buffer[BUFFER_SIZE];
 while (fgets(buffer, sizeof buffer, stream) != NULL)
 {
   // process buffer
 }
 if (feof(stream))
 {
   // hit end of file
 }
 else
 {
   // some other error interrupted the read
 }

使用的fscanf()

char buffer[BUFFER_SIZE];
while (fscanf(stream, "%s", buffer) == 1) // expect 1 successful conversion
{
  // process buffer
}
if (feof(stream)) 
{
  // hit end of file
}
else
{
  // some other error interrupted the read
}

使用龟etc()

int c;
while ((c = fgetc(stream)) != EOF)
{
  // process c
}
if (feof(stream))
{
  // hit end of file
}
else
{
  // some other error interrupted the read
}

使用 FREAD()

char buffer[BUFFER_SIZE];
while (fread(buffer, sizeof buffer, 1, stream) == 1) // expecting 1 
                                                     // element of size
                                                     // BUFFER_SIZE
{
   // process buffer
}
if (feof(stream))
{
  // hit end of file
}
else
{
  // some other error interrupted read
}

请注意,该表格是为所有这些相同:检查读取操作的结果;如果失败,然后的检查EOF。你会看到很多的例子,如:

Note that the form is the same for all of them: check the result of the read operation; if it failed, then check for EOF. You'll see a lot of examples like:

while(!feof(stream))
{
  fscanf(stream, "%s", buffer);
  ...
}

这个形式不工作的人认为它的方式,因为的feof()直到的之后的你已经不会返回true尝试读取过去的文件的末尾。其结果是,循环执行一次太多,这可能会或可能不会引起你一些悲痛。

This form doesn't work the way people think it does, because feof() won't return true until after you've attempted to read past the end of the file. As a result, the loop executes one time too many, which may or may not cause you some grief.

这篇关于如何使用EOF通过在C文本文件来运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆