使用int feof时的问题(FILE * fp); [英] a problem when using int feof(FILE *fp);

查看:112
本文介绍了使用int feof时的问题(FILE * fp);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码检查a.txt是否有完全相同的行,然后写入

不同的行到b.txt


这里是:

(在此之前,你应该创建一个.txt)

----------------------

#include< stdio.h>

#include< string.h>

void main(){

FILE * fp,* ftemp;

char ch [50],comp [50];

int lflag;


fp = fopen (a.txt,r);

ftemp = fopen(" b.txt"," w +");


/ *以下检查fp是否具有完全相同的行,然后将
不同的行写入ftemp * /

while(!feof(fp)){

fgets(ch,50,fp);

lflag = 1;

rewind(ftemp); / *文件点指向ftemp的开头* /

while(!feof(ftemp)){

fgets(comp,50,ftemp);

if(!strcmp(ch,comp)){

lflag = 0;

break;

}

} / *检查ftemp中的所有行。如果有一个字符串等于ch,那么

make lflag = 0; * /

if(lflag){fseek(ftemp,0,2);

fputs(ch,ftemp);

puts(ch);

} / *如果lflag == 1,则将字符串ch写入ftemp,并且输出到

屏幕。* /

}


fclose(fp);

fclose (ftemp);

}

------------------------


当a.txt有一些字符时,我们得到正确的结果;

但是当a.txt为空时,我们可以在b.txt
,并在屏幕上:

ììììììììììììììììììììììììììììììììììììììììììììììììììIIP乙

现在又来了一个问题:

如果FP点到空文件,然后是feof(fp)应该等于

1,那么它怎么能进入呢?


我已经好几天了。所以 -


最后每个文件都有EOF吗?(我发现空文件是0k)

文件打开时,文件指向第一个字符吗?

第一个字符和最后一个字符都是空文件中的EOF吗?

希望得到你的帮助!

解决方案

kindrain写道:


代码检查a.txt是否有完全相同的行,然后写

不同的行进入b.txt


这是:

(在此之前,你应该创建一个.txt)

--- -------------------

#include< stdio.h>

#include< string.h>

void main(){



这不可移植。使用int main(void)并从main返回0或

EXIT_FAILURE。对于EXIT_FAILURE,你需要stdlib.h。


FILE * fp,* ftemp;

char ch [50],comp [50];

int lflag;


fp = fopen(" a.txt"," r");

ftemp = fopen(" b.txt"," w +");


/ *以下检查fp是否有完全相同的行,然后写

不同的行进入ftemp * /

while(!feof(fp)){



这显然是一个常见的初学者错误。在C中,函数feof和

ferror应该* *之前* I / O函数(比如

fgetc / fgets等)尝试读取流。


而是调用你的I / O函数和*如果*该函数表明它已经失败了(通过返回EOF或NULL或记录), *然后*你可以

调用feof或ferror *消除由于文件结束条件或读取错误导致的故障是否为
之间的歧义*。 br />


fgets(ch,50,fp);

lflag = 1;

rewind(ftemp); / *文件点指向ftemp的开头* /

while(!feof(ftemp)){



Ditto。


fgets(comp,50,ftemp);

if(!strcmp(ch,comp)){

lflag = 0;

休息;

}

} / *检查ftemp中的所有行。如果有一个字符串等于ch,那么

make lflag = 0; * /

if(lflag){fseek(ftemp,0,2);

fputs(ch,ftemp);

puts(ch);

} / *如果lflag == 1,则将字符串ch写入ftemp,并且输出到

屏幕。* /

}


fclose(fp);

fclose (ftemp);

}

------------------------


当a.txt有一些字符时,我们得到正确的结果;

但是当a.txt为空时,我们可以在b.txt
,并在屏幕上:

ììììììììììììììììììììììììììììììììììììììììììììììììììIIP乙


现在又来了一个问题:

如果fp指向空文件,然后是feof(fp)。应该等于

1,那么它怎么能进入呢?



这是因为你误解了一个人必须在阅读之前用feof

来检查。在C中,它是另一种方式。首先尝试阅读和

如果读取功能指示失败,那么检查是否因为文件结尾是否为b / b
,如果是由于阅读

错误。


我已经想好几天了。所以 -



如果您已经阅读过feof或ferror的文档或任何基本的C / /
教科书(如K& R2),那么您就不必浪费时间了。


每个文件到底有没有EOF?(我发现空文件是0k)



EOF是一个宏它解析为负的int值。它由许多C'的I / O函数返回

来表示*任何*类型的故障。返回

值的EOF确实*不*意味着遇到了文件结尾。

只有当feof返回true时才意味着这样。


文件点是否指向文件打开时的第一个字符?



如果尚未在附加模式下打开则是。


是第一个字符和最后一个字符在一个空文件中将两个EOF都加入?



从空文件中读取将返回EOF。用feof

进一步测试将返回true。该文件实际包含的是实现

定义。


希望得到你的帮助!


7月12日,4:44 * pm,santosh< santosh .... @ gmail.comwrote:


kindrain写道:


代码检查a.txt是否有完全相同的行,然后写

不同的行到b.txt


这是:

(在此之前,你应该创建a.txt)

----------------------

#include< stdio.h>

#include< string.h>

void main(){



这不可移植。使用int main(void)并从main返回0或

EXIT_FAILURE。对于EXIT_FAILURE需要stdlib.h中

FILE * fp的,* ftemp;

炭CH [50],排版[50];

int lflag;


FP =的fopen(QUOT; A.TXT"," R");

ftemp =的fopen(QUOT ; b.txt"," W +");


/ *以下检查fp是否具有完全相同的行,然后将
不同的行写入ftemp * /

while(!feof(fp)){



这显然是一个常见的初学者错误。在C中,函数feof和

ferror应该* *之前* I / O函数(比如

fgetc / fgets等)尝试读取流。


而是调用你的I / O函数和*如果*该函数表明它已经失败了(通过返回EOF或NULL或记录), *然后*你可以

调用feof或ferror *消除由于文件结束条件或读取错误导致的故障是否为
之间的歧义*。 br />


fgets(ch,50,fp);

lflag = 1;

rewind(ftemp); / *文件点指向ftemp的开头* /

while(!feof(ftemp)){



Ditto。



fgets(comp,50,ftemp);

if(!strcmp(ch,comp)){

lflag = 0;

休息;

}

} / *检查ftemp中的所有行。如果有一个字符串等于ch,那么

make lflag = 0; * /

if(lflag){fseek(ftemp,0,2);

fputs(ch,ftemp);

puts(ch);

} / *如果lflag == 1,则将字符串ch写入ftemp,并且输出到

屏幕。* /

}


fclose(fp);

fclose(ftemp);

}

--------------------- ---


当a.txt有一些字符时,我们得到正确的结果;

但是当a.txt时是空的,我们可以看到无论是在b.txt

和在屏幕下面的话:

ììììììììììììììììììììììììììììììììììììììììììììììììììIIP乙

现在这里有一个问题:

如果fp指向一个空文件,那么feof(fp)应该等于

1,那么它怎么能进入呢?



这是因为你误解了一个人必须在阅读之前用feof

来检查。在C中,它是另一种方式。首先尝试阅读和

如果读取功能指示失败,那么检查是否因为文件结尾是否为b / b
,如果是由于阅读

错误。


我已经好几天了。所以 -



如果您已经阅读了文件中的feof或ferror或任何基本的C / /
教科书,如K& R2那么你就不必浪费时间了。


每个文件到底有没有EOF?(我发现空文件是0k)



EOF是一个宏它解析为负的int值。它由许多C'的I / O函数返回

来表示*任何*类型的故障。返回

值的EOF确实*不*意味着遇到了文件结尾。

只有当feof返回true时才意味着这样。


文件点是否指向文件打开时的第一个字符?



如果尚未在附加模式下打开,则为是。


是第一个字符和最后一个字符在一个空文件中将两个EOF都加入?



从空文件中读取将返回EOF。用feof

进一步测试将返回true。该文件实际包含的是实现

定义。


希望得到你的帮助! - 隐藏引用的文字 -


- 显示引用文字 - 隐藏引用文字 -


- 显示引用文字 -



所以文件中没有EOF吗?

但是feof是如何工作的?它首先读取一个char,然后返回true,如果

失败,否则返回零?


kindrain写道:


< snip>


所以文件中没有EOF?



正如我所说的,EOF是一个在stdio.h中定义的宏,它评估为由几个类型返回的int类型的负值

C'的标准I / O

用于发出信号* *文件结尾*或*其他一些错误。哪个

其中一个特定失败的呼叫是由

在调用后用feof或ferror *测试确定的。


但是feof是如何工作的?



标准并没有准确说明实际上(或者是恐怖的)
是如何运作的。这取决于实现。概念上feof和ferror

将分别测试流

的文件结束和错误指示,并在设置时返回true,否则返回false。


这些指标将被流上调用的前一个I / O

函数设置(或清除)。


< snip>


the code checks whether a.txt has exact the same lines, then write
different lines into b.txt

Here it is:
(before that ,you should creat a.txt)
----------------------
#include<stdio.h>
#include<string.h>
void main(){
FILE *fp,*ftemp;
char ch[50],comp[50];
int lflag;

fp=fopen("a.txt","r");
ftemp=fopen("b.txt","w+");

/*the following checks whether fp has exact the same lines, then write
different lines into ftemp*/
while(!feof(fp)){
fgets(ch,50,fp);
lflag=1;
rewind(ftemp); /*the file point points to the beginning of ftemp*/
while(!feof(ftemp)){
fgets(comp,50,ftemp);
if(!strcmp(ch,comp)){
lflag=0;
break;
}
} /*check all lines in ftemp. if there is a string equals to ch,then
make lflag=0;*/
if(lflag){ fseek(ftemp,0,2);
fputs(ch,ftemp);
puts(ch);
} /*if lflag==1,then write string ch into ftemp, and output to the
screen.*/
}

fclose(fp);
fclose(ftemp);
}
------------------------

when a.txt has some chars, we get the right result;
but when a.txt is empty, we can see the following words both in b.txt
and in the screen:
ìììììììììììììììììììììììììììììììììììììììììììììììììì ììP?B
now here comes a question:
if fp points to an empty file,then "feof(fp)" should be equal to
1,then how can it step into while?

I''ve thought it for several days.so--

Does every file has an EOF in the end?(I find empty files are 0k)
Does the file point points to the first char when the file is opened?
Are the first char and last char both EOF in an empty file?
Hoping for your help!

解决方案

kindrain wrote:

the code checks whether a.txt has exact the same lines, then write
different lines into b.txt

Here it is:
(before that ,you should creat a.txt)
----------------------
#include<stdio.h>
#include<string.h>
void main(){

This is not portable. Use int main(void) and return either 0 or
EXIT_FAILURE from main. For EXIT_FAILURE you need stdlib.h.

FILE *fp,*ftemp;
char ch[50],comp[50];
int lflag;

fp=fopen("a.txt","r");
ftemp=fopen("b.txt","w+");

/*the following checks whether fp has exact the same lines, then write
different lines into ftemp*/
while(!feof(fp)){

This is apparently a common beginner error. In C the function feof and
ferror should *not* be called *before* an I/O function (like
fgetc/fgets etc) has tried to read a stream.

Rather call your I/O function and *if* that function indicates that it
has failed (by returning EOF or NULL or as documented), *then* you can
call feof or ferror to *disambiguate* between whether the failure was
due to end-of-file condition or due to a read error.

fgets(ch,50,fp);
lflag=1;
rewind(ftemp); /*the file point points to the beginning of ftemp*/
while(!feof(ftemp)){

Ditto.

fgets(comp,50,ftemp);
if(!strcmp(ch,comp)){
lflag=0;
break;
}
} /*check all lines in ftemp. if there is a string equals to ch,then
make lflag=0;*/
if(lflag){ fseek(ftemp,0,2);
fputs(ch,ftemp);
puts(ch);
} /*if lflag==1,then write string ch into ftemp, and output to the
screen.*/
}

fclose(fp);
fclose(ftemp);
}
------------------------

when a.txt has some chars, we get the right result;
but when a.txt is empty, we can see the following words both in b.txt
and in the screen:
ìììììììììììììììììììììììììììììììììììììììììììììììììì ììP?B
now here comes a question:
if fp points to an empty file,then "feof(fp)" should be equal to
1,then how can it step into while?

This proceeds from your misconception that one must check with feof
before reading. In C it is the other way around. First try to read and
if the read function indicates failure then check with feof if it is
because of end-of-file or check with ferror if it was due to a read
error.

I''ve thought it for several days.so--

If you had read the documentation for feof or ferror or any elementary C
textbook like K&R2 then you needn''t have wasted days on this.

Does every file has an EOF in the end?(I find empty files are 0k)

EOF is a macro which resolves to a negative int value. It is returned by
many of C''s I/O function to indicate *any* type of failure. A return
value of EOF does *not* mean that end-of-file has been encountered.
Only if feof returns true does it mean so.

Does the file point points to the first char when the file is opened?

If it has not been opened in append mode then yes.

Are the first char and last char both EOF in an empty file?

A read from an empty file will return EOF. Further testing with feof
will return true. What the file actually contains is implementation
defined.

Hoping for your help!


On Jul 12, 4:44*pm, santosh <santosh....@gmail.comwrote:

kindrain wrote:

the code checks whether a.txt has exact the same lines, then write
different lines into b.txt

Here it is:
(before that ,you should creat a.txt)
----------------------
#include<stdio.h>
#include<string.h>
void main(){


This is not portable. Use int main(void) and return either 0 or
EXIT_FAILURE from main. For EXIT_FAILURE you need stdlib.h.

FILE *fp,*ftemp;
char ch[50],comp[50];
int lflag;

fp=fopen("a.txt","r");
ftemp=fopen("b.txt","w+");

/*the following checks whether fp has exact the same lines, then write
different lines into ftemp*/
while(!feof(fp)){


This is apparently a common beginner error. In C the function feof and
ferror should *not* be called *before* an I/O function (like
fgetc/fgets etc) has tried to read a stream.

Rather call your I/O function and *if* that function indicates that it
has failed (by returning EOF or NULL or as documented), *then* you can
call feof or ferror to *disambiguate* between whether the failure was
due to end-of-file condition or due to a read error.

fgets(ch,50,fp);
lflag=1;
rewind(ftemp); /*the file point points to the beginning of ftemp*/
while(!feof(ftemp)){


Ditto.


fgets(comp,50,ftemp);
if(!strcmp(ch,comp)){
lflag=0;
break;
}
} /*check all lines in ftemp. if there is a string equals to ch,then
make lflag=0;*/
if(lflag){ fseek(ftemp,0,2);
fputs(ch,ftemp);
puts(ch);
} /*if lflag==1,then write string ch into ftemp, and output to the
screen.*/
}

fclose(fp);
fclose(ftemp);
}
------------------------

when a.txt has some chars, we get the right result;
but when a.txt is empty, we can see the following words both in b.txt
and in the screen:
ìììììììììììììììììììììììììììììììììììììììììììììììììì ììP?B

now here comes a question:
if fp points to an empty file,then "feof(fp)" should be equal to
1,then how can it step into while?


This proceeds from your misconception that one must check with feof
before reading. In C it is the other way around. First try to read and
if the read function indicates failure then check with feof if it is
because of end-of-file or check with ferror if it was due to a read
error.

I''ve thought it for several days.so--


If you had read the documentation for feof or ferror or any elementary C
textbook like K&R2 then you needn''t have wasted days on this.

Does every file has an EOF in the end?(I find empty files are 0k)


EOF is a macro which resolves to a negative int value. It is returned by
many of C''s I/O function to indicate *any* type of failure. A return
value of EOF does *not* mean that end-of-file has been encountered.
Only if feof returns true does it mean so.

Does the file point points to the first char when the file is opened?


If it has not been opened in append mode then yes.

Are the first char and last char both EOF in an empty file?


A read from an empty file will return EOF. Further testing with feof
will return true. What the file actually contains is implementation
defined.

Hoping for your help!- Hide quoted text -


- Show quoted text -- Hide quoted text -

- Show quoted text -


so no EOF in files?
but how does feof work? does it first read a char,then return true if
it fails,else return zero?


kindrain wrote:

<snip>

so no EOF in files?

As I said EOF is a macro defined in stdio.h that evaluates to a negative
value of type int that is returned by several of C''s standard I/O
functions to signal *either* end-of-file *or* some other error. Which
one of these is the case for a particular failed call is determined by
testing with feof or ferror *after* the call.

but how does feof work?

The Standard doesn''t precisely spell out how feof (or ferror) actually
works. That is upto the implementations. Conceptually feof and ferror
will test the end-of-file and error indicators for the stream
respectively and return true when they are set or false otherwise.

These "indicators" would''ve been set (or cleared) by the previous I/O
function called on the stream.

<snip>


这篇关于使用int feof时的问题(FILE * fp);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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