101
{ USER }
posts: 55
last: 09-Apr-2008
TITLE: Merging files into one new file
DESCRIPTION: Copying one or more than one file into one file
Submitted: 09-Apr-2008 09:56:12 ( 38w 5d 19h ago ) Language: C++ (*.cpp *.h)
Views: 98 Lines of Code: 75 LINES
Rating:
rate: star1
star2
star3
star4
star5
dstar1
dstar2
dstar3
dstar4
dstar5  ( rated! )
  { 0.00 / 5 }
Difficulty: Advanced
Bookmark
/* Author: 101
   Date: 09-04-2008
   Filename: 
   Description: Copying one or more than one file into one file
   History: 
*/


#include <stdio.h>

void main(int argc,char *argv[]){
     int i=0,
	 is_copied=0;

     FILE *input,
	  *output;

     char ch;

     clrscr();


     if (argc>=3){

	     /* last file is the output file */
	     output=fopen(argv[argc-1],"w+");
	     if (output==NULL){
		 gotoxy(10,1);
		 printf("Can't open output file :%s",argv[argc-1]);
		 getch();
		 return;
	     }


	     /* input files */
	     for(i=1;i<argc-1;i++){
		 input=fopen(argv[i],"r");

		 if (input==NULL){
			gotoxy(10,i+2);
			printf("Can't open input file :%s",argv[i]);
		 }
		 else{
		      /* writing from input file to output file */

		       rewind(input);
		       while((ch=fgetc(input))!=EOF){
			      fprintf(output,"%c",ch);
			      is_copied=1;
		       }

		       gotoxy(10,i+2);

		       if (is_copied)
			       printf("%s successfully copied into %s",argv[i],argv[argc-1]);
		       else
			       printf("%s can't copy into %s",argv[i],argv[argc-1]);

		       is_copied=0;

		 }
                 fclose(input);
	    }

	    fclose(output);


      }
      else{
	  gotoxy(10,1);
	  printf("please specifiy at least two file one source and another target");
      }

      getch();
}