Thursday 9 February 2012

concatenate command line program

Here is my approach for creating a concatenation command line program.
Don't try on matrix...
It's not perfect because I'm too busy to work on it any longer!!
If you have any suggestion please leave a comment!!!!!




#include <iostream>


using namespace std;

bool Files(int argc,char* argv[]);


int main(int argc, char* argv[])
{

bool done;

done= true;

  (argc < 3)
 &&
cout<<"Usage:  Concat  Destination.txt File1.txt (File2.txt File3.txt ...)"<<endl
 &&
cout<<"       make sure you have File1.txt and File2.txt exists!!!"<<endl
 &&
 (done = !done);


  (!!done)
 &&
    Files(argc,argv)
&&
cout<<"Concatenation successful!!"<<endl;

//this is just so the program won't exit before we see the "cout"s..
  getchar();

  return 0;
}


bool Files(int argc,char* argv[])
{

  FILE* destination ;
  FILE* files;
  int i;
  int temp;


  //first argument is the destination file always
     destination = fopen(argv[1], "a");

for(i= 2; i< argc; i++)
{
 files = fopen(argv[i], "r");
 while((temp = fgetc(files)) != EOF)
 {
fputc(temp, destination );
 }
 fclose(files);
}

fclose(destination);

return true;
}

No comments:

Post a Comment