Can someone help me to write a code that uses command line arguments argc & argv to take a file as an i/p ,open it and copy it to another file?
Thanx,
Gagig
Can someone help me to write a code that uses command line arguments argc & argv to take a file as an i/p ,open it and copy it to another file?
Thanx,
Gagig
I am sorry, I already got the code.
It's like this,
int main( int argc , char *argv[] )
{
FILE *stream,*dest;
char ch;
stream = fopen(argv[1],"rt");
dest = fopen("dest.txt","wt");
while(!feof(stream))
{
ch = fgetc(stream);
fwrite(&ch,sizeof(char),1,dest);
}
}
But is there even more a simpler way?
Thanx,
Gagig eek:
You'd better test your code, because it seems to me that your output file will be 1 byte bigger than your input file.
Yes, u r right. Whenever I do fwrite, the o/p file has an addtional character. Why is it so? How can I eliminate this?
Thanx,
gagig
Check for EndOfFile condition after you read, but before you write.
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
Well instead of the
fwrite(&ch,sizeof(char),1,dest);
statement why not use this
fputc(ch,dest);
Code:int ch; // yes, it must be int, to check for EOF properly while ( (ch = fgetc(stream)) != EOF ) { // or fputc as Silverdream mentioned fwrite(&ch,sizeof(char),1,dest); }
Hi salem,
Thanx for the code..
Gagig