这样一个程序,实现的是打开一个文本文件将其内容逆序过来!
#include<stdio.h>
#define BUF 1
main()
{
int i=0;//buf
long j=0;//text
unsigned char buf[BUF],text[100];
FILE *fp_o,*fp_n;
fp_o=fopen("1kb.txt","r");
fp_n=fopen("1kb_c.txt","w");
if((fp_o && fp_n)==NULL)//open file
{
printf("can't open\n");
return(0);
}
for(;!feof(fp_o);i++)
{
fread(buf,1,1,fp_o);
text[i]=buf[0];
}
printf("\nnum:%d\n",i);
for(i--;i>=0;i--)
{
buf[0]=text[i];
fwrite(buf,1,1,fp_n);
}
fclose(fp_o);
fclose(fp_n);
return(1);
}
这里面的这个FEOF函数是什么意思?好像在逆序过程的末尾是不是要执行2次?
比如:原文件是12
输出的逆序文件应该是21,可是却成了221了。
如果不用这个函数,那里应该怎么改啊???
第1个回答 2008-03-07
函数名: feof
功 能: 检测流上的文件结束符
用 法: int feof(FILE *stream);
程序例:
#include <stdio.h>
int main(void)
{
FILE *stream;
/* open a file for reading */
stream = fopen("DUMMY.FIL", "r");
/* read a character from the file */
fgetc(stream);
/* check for EOF */
if (feof(stream))
printf("We have reached end-of-file\n");
/* close the file */
fclose(stream);
return 0;
}
功 能: 检测流上的文件结束符
用 法: int feof(FILE *stream);
程序例:
#include <stdio.h>
int main(void)
{
FILE *stream;
/* open a file for reading */
stream = fopen("DUMMY.FIL", "r");
/* read a character from the file */
fgetc(stream);
/* check for EOF */
if (feof(stream))
printf("We have reached end-of-file\n");
/* close the file */
fclose(stream);
return 0;
}
第2个回答 2008-03-07
int feof( FILE *stream );
The function feof() returns a nonzero value if the end of the given file stream has been reached.
如果文件到达结尾,就返回非0值本回答被提问者采纳
The function feof() returns a nonzero value if the end of the given file stream has been reached.
如果文件到达结尾,就返回非0值本回答被提问者采纳
第3个回答 2008-03-07
FEOF()函式测试档案的指标是否指到档尾 (End of File)。
若是成功或发生错误则传回 true 值,其它情形传回 false 值。
若是成功或发生错误则传回 true 值,其它情形传回 false 值。
第4个回答 2008-03-07
判断文件是否结束.........