Thursday 1 March 2012

Test 1 – 20113 Q2

#include 
#include 
#include 
using namespace std;
 
int main(int argc,char* argv[])
{
    FILE *fs1;
    FILE *fs2;
    FILE *ft;
    char ch;
    int i;
    if ( argc != 4 ) /* argc should be 4 for correct execution */
      {      
        printf( "Arguments provided are not sufficiet\n");  
      }
    else{
    fs1 = fopen( argv[2], "r" );
    fs2 = fopen( argv[3], "r" );
 
    if( fs1 == NULL || fs2 == NULL )
     {
       perror("Error ");
       exit(EXIT_FAILURE);//EXIT_FAILURE -- unsuccessful termination

     }
 
    ft = fopen(argv[1],"w");
 
    if( ft == NULL )
     {
       perror("Error ");
       exit(EXIT_FAILURE);
     }
 
    while( fscanf(fs1,"%c", &ch) != EOF )
    fprintf(ft,"%c", ch);
      
    while( fscanf(fs2,"%c", &ch)  != EOF )
       fprintf(ft,"%c", ch);
  
 
    printf("Two files were merged into %s file successfully.\n",argv[1]);
 
    fclose(fs1);
    fclose(fs2);
    fclose(ft);  
  
   }
   return 0;
}

No comments:

Post a Comment