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

查看:20
本文介绍了如何使用 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
}

使用fgetc():

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,直到 after 你试图读到最后的文件.结果,循环执行了一次太多,这可能会也可能不会让您感到悲伤.

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天全站免登陆