简体   繁体   English

从文件读取整数

[英]Reading integers from a file

Using the feof() function, on my output file i have the last value printed twice. 使用feof()函数,在我的输出文件中,我将最后一个值打印了两次。 How can i avoid that? 我如何避免这种情况? My output should be 我的输出应该是

12
6
15
13

and i have 我有

12
6
15
13
13

Thank you! 谢谢!

while(!feof(pfile1))
{       
    sum=0;
    fscanf(pfile1,"%d%d%d",&choice,&day,&val);
    if(choice==0)
    {
        i=day-1;
        a[i]=a[i]-val;
    }
    else if(choice==1)
    {
        for(i=day-1;i<=val-1;i++)
        {
            sum=sum+a[i];

        }
        fprintf(pfile2,"%d\n",sum);     
    }
}

That's because the feof flag will only be set, as soon as you read PAST the end of a file. 这是因为feof标志只会在您读取PAST文件末尾时设置。 To remedy this problem, replace the feof with the fscanf from below and check for the number of arguments. 要解决此问题,请从下面用fscanf替换feof,并检查参数数量。

If it's lower than 3, you know that some error occurred...for example the end of file was reached. 如果小于3,则说明发生了某些错误...例如到达文件末尾。

In fact, you should avoid using feof for the above mentioned reason whenever possible. 实际上,由于上​​述原因,应尽可能避免使用feof。

To underline this with some code: 用一些代码强调这一点:

while(fscanf(pfile1,"%d%d%d",&choice,&day,&val) == 3) {
    //Continue reading
}
fclose(pfile1);
fclose(pfile2);

The posted code will not detect feof() until after it has processed an unsuccessful read, resulting in the behaviour you witness. 在处理不成功的读取之后,发布的代码将不会检测到feof() ,从而导致您看到的行为。

You need to check feof() immediately after the call to fscanf() or check the result of fscanf() which returns the number of assignments made: 你需要检查feof()调用后立即fscanf()或检查的结果fscanf()返回的转让次数:

if (3 == fscanf(pfile1,"%d%d%d",&choice,&day,&val))
{
    /* All three integers successfully read. */
}

Another possible structure of the loop (as suggested in the comments by MichalAnderson): 循环的另一种可能结构(如MichalAnderson的评论中所建议):

while (3 == fscanf(pfile1,"%d%d%d",&choice,&day,&val))
{
}

One option is to check the return value of fscanf : 一种选择是检查fscanf的返回值:

On success, the function returns the number of items successfully read. 成功时,该函数返回成功读取的项目数。 This count can match the expected number of readings or be less -even zero- in the case of a matching failure. 在匹配失败的情况下,此计数可以与预期的读数数匹配,或者小于甚至为零。 In the case of an input failure before any data could be successfully read, EOF is returned. 如果在输入数据失败之前无法成功读取任何数据,则返回EOF。

So, something like: 因此,类似:

if( fscanf(pfile1,"%d%d%d",&choice,&day,&val) <= 0 )
{
    break;
}

The other option is to check for feof after executing fscanf . 另一个选项是在执行fscanf之后检查feof For example: 例如:

if( feof( pfile1 ) )
{
    return; // or something
}
do
{
    // the same loop body
}
while( ! feof( pfile1 ) );

You should check the number of read elments, as returned by fscanf : if it's not what you're waiting for (3), you should break from the loop. 您应该检查fscanf返回的已读取元素的数量:如果不是您要等待的内容(3),则应该退出循环。

In this case, you're simply working two times on the last read line. 在这种情况下,您只需在最后一条读取行上进行两次操作即可。

From the documentation : 文档中

RETURN VALUE 返回值

Upon successful completion, these functions shall return the number of successfully matched and assigned input items; 成功完成后,这些功能应返回成功匹配和分配的输入项的数量;

fscanf(pfile1,"%d%d%d",&choice,&day,&val);

Here you are reading the integers from an input file. 在这里,您正在从输入文件中读取整数。 fscanf will scan the file until it reaches the EOF. fscanf将扫描文件,直到到达EOF。 Each time it reads three integers it returns 3 . 每次读取三个整数都返回3。 If the value returned by fsacnf is less than 3 then it automatically assignes the buffer value. 如果fsacnf返回的值小于3,则它将自动分配缓冲区值。 SO this is the reason for getting the repeated value in the output. 因此,这就是在输出中获得重复值的原因。

If the program is edited with checking the return value of fscanf() for value 3 and then executing the if else statement in regard of choice will give the desired output. 如果通过检查fscanf()的返回值是否为3来编辑程序,然后执行关于select的if else语句,将提供所需的输出。

Example 1:- 示例1:

Consider the input file with content:- 考虑输入文件的内容:

0 2 3 0 2 3

1 1 2 1 1 2

(--blank--) //here the fscanf will scan for an integer but since its a blank line it will reach the EOF , and returns -1 and uses the buffer value (previous values) (--blank--) //此处fscanf将扫描整数,但由于其为空行,它将到达EOF,并返回-1并使用缓冲区值(以前的值)

Then the output file :- 然后输出文件:

118 118

118 118

Example 2:- 示例2:

Input File (without blank lines):- 输入文件(无空行):-

0 2 3 0 2 3

1 1 2 1 1 2

Output File:- 输出文件:-

118 118

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM