Thread: Command line arguments for file

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    10

    Unhappy Command line arguments for file

    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

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    10

    Re: Command line arguments for file

    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:

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,534
    You'd better test your code, because it seems to me that your output file will be 1 byte bigger than your input file.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    10
    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

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    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.

  6. #6
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53
    Well instead of the

    fwrite(&ch,sizeof(char),1,dest);

    statement why not use this

    fputc(ch,dest);


  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,534
    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); 
    }

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    10

    Cool

    Hi salem,
    Thanx for the code..

    Gagig

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  2. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  3. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  4. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  5. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 02:11 PM