Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
i want to copy contents in one file in to another file.

C
fPout=fopen("parse.txt","r")
fPin=fopen("dpl.txt","w")
 
 do
 {
       a = fgetc(fPout);
      fputc(a,fPin);
 }
    while(a!=EOF);


i'm using this method but it shows error while compiling like multiple declaration,type mismatch etc. Also i've tried fread function but it doesn't work please help.....
Posted
Updated 9-Jun-15 22:53pm
v3

I would use a ssytem facility for just copying files (e.g. system("cp filein fileout"); on linux). Anyway the correct code to accomplish that should be
C#
#include <stdio.h>

int main()
{
  FILE * fpin, *fpout;

  int i;

  fpin = fopen("parse.txt", "r");
  if ( !fpin )
  {
    // handle error here
  }
  fpout = fopen("dpl.txt", "w");
  if (!fpout)
  {
    // handle error here
  }

  while ( (i = fgetc(fpin) ) != EOF )
  {
    fputc(i, fpout);
  }

  fclose(fpout);
  fclose(fpin);

  return 0;
}



Please note that errors on fopen must be properly handled (e.g. in all but trivial test programs, the acquired resources must be released).
 
Share this answer
 
v2
Look at your variable names, you use fPout for the input file, and fPin for the output file. You then try to read from a non-existent file referred to as fPParse. You also say i've tried fread function but it doesn't work, which tells us nothing; and having used fread extensively myself, I know that it does work.
 
Share this answer
 
Don't do it; it could be prohibitively slow on some bigger file. Read and write bytes by big chunks:
http://www.techonthenet.com/c_language/standard_library_functions/stdio_h/fread.php[^],
http://www.techonthenet.com/c_language/standard_library_functions/stdio_h/fwrite.php[^].

Be careful to check up the return value of fread. It tells you the number of actually read bytes. As soon as your number of actually read bytes is less than the requested block size, it indicates that this is the last block. This way, you don't even need a check for end of file.

How big should be the block? It depends. On modern PC systems, it could be a megabyte, several megabytes, something like that.

—SA
 
Share this answer
 
v2

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