Click here to Skip to main content
11,715,491 members (86,201 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C
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+");
    printf("couldn't open File...\n");

  fseek(rfile, 0, SEEK_END);
  size_t file_size = ftell(rfile);

  FILE* pFile = fopen ( "newfile.txt" , "w+" );
  fwrite (buffer , 1 ,sizeof(buffer) , pFile );
  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 9-Apr-13 20:50pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The first thing you need to do is read the documentation on 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. Thumbs Up | :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 Thumbs Up | :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:
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.
CPallini at 10-Apr-13 3:33am
Actually he did a SEEK_SET, before reading.
OriginalGriff at 10-Apr-13 3:42am
Caffeine, I need more caffeine...
nv3 at 10-Apr-13 3:42am
You beat me by a minute; obviously you had more coffee this morning than I had :-) 5.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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

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);
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 (rfile);
return 0;

note, for the sake of brevity, I'm still missing some error checking.
nv3 at 10-Apr-13 3:44am
Good job. You could have done it with a fixed size buffer and a loop, though.
Aayman Khalid at 10-Apr-13 4:24am
Thank you:)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 518
1 Richard MacCutchan 300
2 Maciej Los 259
3 ManasKumarM 155
4 Frankie-C 125
0 Sergey Alexandrovich Kryukov 785
1 CHill60 415
2 Richard MacCutchan 405
3 Maciej Los 389
4 OriginalGriff 320

Advertise | Privacy | Mobile
Web03 | 2.8.150901.1 | Last Updated 10 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100