关于feof()的问题 [英] a question about feof()
本文介绍了关于feof()的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当feof()决定fread()的结束时,为什么文件被复制两次。
PS:复制的文件大小等于缓冲区的大小。
#include < stdio.h >
#include < stdlib.h >
#define BUFSIZE 100
int main( int argc, char * argv [])
{
if (argc!= 3 )
{
printf( please按照以下格式输入:
programeName sourceName DestName!\ n< /跨度>);
return 0 ;
}
FILE * pS,* pD;
char * sfName,* dfName;
char cbuffer [BUFSIZE] = { 0 };
sfName = argv [ 1 ];
dfName = argv [ 2 ];
if ((pS = fopen(sfName, rb))== NULL)
{
printf( 打开soucefile%s不成功!\ n
也许是源文件无效!,sfName);
退出( 1 );
}
if ((pD = fopen(dfName, wb))== NULL)
{
printf( 打开destfile%s不成功!\ n,dfName);
退出( 1 );
}
而(!feof(pS))
{
fread(cbuffer, sizeof (cbuffer), 1 ,pS);
if (ferror(pD))
{
clearerr(pD);
printf( 写操作不成功\ n);
退出( 1 );
}
fwrite(cbuffer, sizeof (cbuffer), 1 ,PD);
if (ferror(pD))
{
clearerr(pD);
printf( 写操作不成功\ n);
退出( 1 );
}
}
fclose(pS);
fclose(pD);
return 0 ;
}
解决方案
考虑到你的读数没有达到真正的EOF - 循环输入两次
或许尝试将读取大小增加到 > 文件大小 - 然后它将返回整个文件并强制命中EOF。
当我以前做这种类型的文件读取时,我做了以下工作:
1)创建了一个大小与磁盘的簇大小(可能不值得麻烦了)
2)循环读取,直到读取的字符数为<读取大小
3)在编写事件(2)后爆发循环,因为没有更多数据可以写入
他们认为在这个和其他事件中考虑:你的测试何时从true变为false?是否使用while循环或do-while循环的相同考虑。设置一个计数器,看看你通过读循环的次数。
When feof() is to decide the end of fread(),why the file is copied twice.
PS: The size of file which is copied equals to the size of the buffer.
#include<stdio.h>
#include<stdlib.h>
#define BUFSIZE 100
int main(int argc,char *argv[])
{
if(argc!=3)
{
printf("please input in accordance with this format:"
" programeName sourceName DestName!\n");
return 0;
}
FILE *pS,*pD;
char *sfName,*dfName;
char cbuffer[BUFSIZE]={0};
sfName=argv[1];
dfName=argv[2];
if((pS=fopen(sfName,"rb"))==NULL)
{
printf("open soucefile %s unsuccessful!\n"
"maybe the soucefile is invalid!",sfName);
exit(1);
}
if((pD=fopen(dfName,"wb"))==NULL)
{
printf("open destfile %s unsuccessful!\n",dfName);
exit(1);
}
while(!feof(pS))
{
fread(cbuffer,sizeof(cbuffer),1,pS);
if(ferror(pD))
{
clearerr(pD);
printf("the write operation is unsuccessful\n");
exit(1);
}
fwrite(cbuffer,sizeof(cbuffer),1,pD);
if(ferror(pD))
{
clearerr(pD);
printf("the write operation is unsuccessful\n");
exit(1);
}
}
fclose(pS);
fclose(pD);
return 0;
}
解决方案
Consider that you haven't hit a true EOF with your read - the loop is entered twice
Perhaps try increasing your read-size to > file size - then it will return the entire file and force a hit the EOF.
When I used to do this type of reading of a file I did the following:
1) created a read buffer of a size consistent with the cluster size of the disk (may not be worth the trouble anymore)
2) read in a loop until the number of character read was < read-size
3) broke out of loop after writing event (2) as there was no more data to write
They thought to consider in this and other events: when is your test changing from true to false? The same consideration of whether to use a while-loop or do-while loop. set a counter to see how many times you go through the read-loop.
这篇关于关于feof()的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文