Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to read a text file and transfer it's contents to another text file in c, Here is my code:

   char buffer[100];

   FILE*  rfile=fopen ("myfile.txt","r+");
   if(rfile==NULL)
  {
    printf("couldn't open File...\n");
  }


  fseek(rfile, 0, SEEK_END);
  size_t file_size = ftell(rfile);
  printf("%d\n",file_size);
  fseek(rfile,0,SEEK_SET);
  fread(buffer,file_size,1,rfile);


  FILE* pFile = fopen ( "newfile.txt" , "w+" );
  fwrite (buffer , 1 ,sizeof(buffer) , pFile );
  fclose(rfile);
  fclose (pFile);
  return 0;
}


the problem that I am facing is the appearence of unnecessary data in the receiving file, I tried the fwrite function with both "sizeof(buffer)" and "file_size",In the first case it is displaying greater number of useless characters while in the second case the number of useless characters is only 3(I checked the value of fread and it is "zero"),I would really appreciate if someone pointed out my mistake and told me how to get rid of these useless characters...
Posted

The first thing you need to do is read the documentation on fread: http://www.cplusplus.com/reference/cstdio/fread/[^] which is pretty clear on what you have to do and even includes an example.
Since you are only allocating a buffer of 100 bytes (and please don't use numbers directly, #define them to a constant so you only set / change them in one place) you cannot read files larger than 100 bytes without using a loop as well. But that is an "exercise for the reader" at this stage.
The parameters to fread are simple:
1) A pointer to the first element of a buffer to receive the data - you have this. :thumbsup:
2) The size of the element to be read - you probably don't have this, as you are setting it to the current position of the stream you just opened, and then moved to probably the end of the file...
3) The number of such elements to read - you set this to one.
4) The file stream :thumbsup:

But of course, it will read no bytes - because you positioned it at the end of teh file before you started reading it...

Instead, don't seek at all, and use fread to read the size of the buffer:
C++
size_t bytesRead = fread(buffer, 1, 100, rfile);
...
fwrite(buffer, 1, bytesRead, pFile);
Only use the constant I suggested instead of the number 100.
And check bytesRead - if it is the size of your buffer, then the file isn't finished, so you need to read some more.
 
Share this answer
 
Comments
CPallini 10-Apr-13 3:33am    
Actually he did a SEEK_SET, before reading.
OriginalGriff 10-Apr-13 3:42am    
Caffeine, I need more caffeine...
nv3 10-Apr-13 3:42am    
You beat me by a minute; obviously you had more coffee this morning than I had :-) 5.
Basically you are writing the full buffer while it is only partially filled. However your code has several flaws, hence I'm trying to rewrite it

C
char * buffer;
FILE*  rfile=fopen ("myfile.txt","r");
if(rfile == NULL)
{
  printf("couldn't open File...\n");
  // here you HAVE TO exit, return or doing whatever appropriate
}
size_t file_size, read_size;
fseek(rfile, 0, SEEK_END);
size_t file_size = ftell(rfile);
fseek(rfile, 0, SEEK_SET);
file_size -= ftell(rfile);
printf("%d\n",file_size);
buffer = (char*) malloc(file_size);
if ( !buffer)
{
  // close the file and exit (or whatever appropriate)
}
read_size = fread(buffer,file_size,1,rfile);

FILE* wfile = fopen ( "newfile.txt" , "w" );
fwrite (buffer, 1, read_size , wfile );
fclose(wfile);
free(buffer);
fclose (rfile);
return 0;


note, for the sake of brevity, I'm still missing some error checking.
 
Share this answer
 
Comments
nv3 10-Apr-13 3:44am    
Good job. You could have done it with a fixed size buffer and a loop, though.
Aayman Khalid 10-Apr-13 4:24am    
Thank you:)
You allocated a fixed size buffer of 100 bytes. If your file is longer you will overrun the buffer end and that will produce undefined results and may even crash your program.

Also: You should only write as much data as you have read and not the buffer size.
 
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