Click here to Skip to main content
15,921,837 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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.
C++
#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;
}
Posted
Updated 22-Oct-13 20:49pm
v2
Comments
Sergey Alexandrovich Kryukov 22-Oct-13 1:30am    
Copied twice? Who told it so? Or, what have you done so it copied twice? Not clear at all.
—SA
ff-8 22-Oct-13 2:55am    
while(!feof()) { fread(buffer,sizeof(buffer),1,pS); fwrite(buffer,sizeof(buffer),1,pD); }
For example: the buffer can contain 5 characters. the source file:aaaaaaaaa\n(10 bytes) after coping,the target file:aaaaaaaaa\naaaa\n(15 bytes).
Sergey Alexandrovich Kryukov 22-Oct-13 3:16am    
Where is copying, at least one? Are you trying to write to the same file where you read?
—SA
Richard MacCutchan 22-Oct-13 8:42am    
Edit your question and show all your code. I have just tried a similar sample and it works correctly.
Richard MacCutchan 22-Oct-13 3:40am    
You need to call feof() on the input stream: feof(pS);

1 solution

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.


 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900