问题描述
代码检查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吗?
希望得到你的帮助!
这不可移植。使用int main(void)并从main返回0或
EXIT_FAILURE。对于EXIT_FAILURE,你需要stdlib.h。
这显然是一个常见的初学者错误。在C中,函数feof和
ferror应该* *之前* I / O函数(比如
fgetc / fgets等)尝试读取流。
而是调用你的I / O函数和*如果*该函数表明它已经失败了(通过返回EOF或NULL或记录), *然后*你可以
调用feof或ferror *消除由于文件结束条件或读取错误导致的故障是否为
之间的歧义*。 br />
Ditto。
这是因为你误解了一个人必须在阅读之前用feof
来检查。在C中,它是另一种方式。首先尝试阅读和
如果读取功能指示失败,那么检查是否因为文件结尾是否为b / b
,如果是由于阅读
错误。
如果您已经阅读过feof或ferror的文档或任何基本的C / /
教科书(如K& R2),那么您就不必浪费时间了。
EOF是一个宏它解析为负的int值。它由许多C'的I / O函数返回
来表示*任何*类型的故障。返回
值的EOF确实*不*意味着遇到了文件结尾。
只有当feof返回true时才意味着这样。
如果尚未在附加模式下打开则是。
从空文件中读取将返回EOF。用feof
进一步测试将返回true。该文件实际包含的是实现
定义。
这不可移植。使用int main(void)并从main返回0或
EXIT_FAILURE。对于EXIT_FAILURE需要stdlib.h中
这显然是一个常见的初学者错误。在C中,函数feof和
ferror应该* *之前* I / O函数(比如
fgetc / fgets等)尝试读取流。
而是调用你的I / O函数和*如果*该函数表明它已经失败了(通过返回EOF或NULL或记录), *然后*你可以
调用feof或ferror *消除由于文件结束条件或读取错误导致的故障是否为
之间的歧义*。 br />
Ditto。
这是因为你误解了一个人必须在阅读之前用feof
来检查。在C中,它是另一种方式。首先尝试阅读和
如果读取功能指示失败,那么检查是否因为文件结尾是否为b / b
,如果是由于阅读
错误。
如果您已经阅读了文件中的feof或ferror或任何基本的C / /
教科书,如K& R2那么你就不必浪费时间了。
EOF是一个宏它解析为负的int值。它由许多C'的I / O函数返回
来表示*任何*类型的故障。返回
值的EOF确实*不*意味着遇到了文件结尾。
只有当feof返回true时才意味着这样。
如果尚未在附加模式下打开,则为是。
从空文件中读取将返回EOF。用feof
进一步测试将返回true。该文件实际包含的是实现
定义。
所以文件中没有EOF吗?
但是feof是如何工作的?它首先读取一个char,然后返回true,如果
失败,否则返回零?
正如我所说的,EOF是一个在stdio.h中定义的宏,它评估为由几个类型返回的int类型的负值
C'的标准I / O
用于发出信号* *文件结尾*或*其他一些错误。哪个
其中一个特定失败的呼叫是由
在调用后用feof或ferror *测试确定的。
标准并没有准确说明实际上(或者是恐怖的)
是如何运作的。这取决于实现。概念上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!
This is not portable. Use int main(void) and return either 0 or
EXIT_FAILURE from main. For EXIT_FAILURE you need stdlib.h.
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.
Ditto.
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.
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.
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.
If it has not been opened in append mode then yes.
A read from an empty file will return EOF. Further testing with feof
will return true. What the file actually contains is implementation
defined.
This is not portable. Use int main(void) and return either 0 or
EXIT_FAILURE from main. For EXIT_FAILURE you need stdlib.h.
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.
Ditto.
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.
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.
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.
If it has not been opened in append mode then yes.
A read from an empty file will return EOF. Further testing with feof
will return true. What the file actually contains is implementation
defined.
- 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?
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.
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);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!