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

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

问题描述

我有一个文本文件,每一行都有字符串。我想为文本文件中的每行增加一个数字,但是当它到达文件的末尾时显然需要停止。我已经尝试过对EOF进行了一些研究,但是不能真正理解如何正确使用它。



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

解决方案

如何检测EOF取决于您用来读取流的方式: / p>

  EOF上的函数结果或错误
-------- --------- -------------
fgets()NULL
fscanf()成功转换次数
小于预期
fgetc()EOF
fread()元素数量
小于预期

检查输入的结果请求上述适当的条件,然后调用 feof()确定结果是否由于触发EOF或其他一些错误。



使用 fgets()

  char buffer [BUFFER_SIZE]; 
while(fgets(buffer,sizeof buffer,stream)!= NULL)
{
//进程缓冲区
}
if(feof(stream))
{
//命中结尾
}
else
{
//其他一些错误中断了
}

使用 fscanf()

  char buffer [BUFFER_SIZE]; 
while(fscanf(stream,%s,buffer)== 1)// expect 1 success conversion
{
//进程缓冲区
}
if (feof(stream))
{
//命中文件结束
}
else
{
//其他一些错误中断了读取

使用 fgetc()

  int c; 
while((c = fgetc(stream))!= EOF)
{
// process c
}
if(feof(stream))
{
//命中结尾
}
else
{
//其他一些错误中断了
}

使用 fread()

  char缓冲区[BUFFER_SIZE]; 
while(fread(buffer,sizeof buffer,1,stream)== 1)// expecting 1
//元素大小
// BUFFER_SIZE
{
//进程缓冲区
}
if(feof(stream))
{
//命中文件结尾
}
else
{
//其他一些错误中断读取
}

请注意,所有这些都是一样的:检查读取操作的结果;如果失败,那么然后检查EOF。你会看到很多例子,如:

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

此表单不符合人们的使用方式认为是这样,因为 之后,您尝试读取文件末尾后, feof()将不会返回true。因此,循环执行一次太多,这可能或可能不会导致您的一些悲伤。


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.

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

解决方案

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

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.

Using 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
 }

Using 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
}

Using fgetc():

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

Using 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
}

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);
  ...
}

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