top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Concat two files using C

+6 votes
288 views

How will write a C program which will concatenate two file and write it third file.

posted Dec 16, 2013 by Neeraj Pandey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
if we can use system command then its simple
main()
{
   system("cat file1 > file3");
   system("cat file2 >> file3");
}

1 Answer

0 votes
void Cat(FILE *input_file, FILE *output_file)
{
   char ch;
   while( ( ch = fgetc(input_file) ) != EOF )
      fputc(ch,output_file);
}

int main ()
{
   FILE *input_file, *output_file;
   ...
   output_file = fopen(<filename 3>, "a+"); // Target file 
   input_file = fopen(<filename 1>, "r");
   Cat(input_file, output_file);
   fclose(input_file);

   input_file = fopen(<filename 2>, "r");
   Cat(input_file, output_file);
   fclose(input_file);
   fclose(output_file);
}
answer Dec 16, 2013 by Satish Mishra
...