Command Line arguments question

This is a discussion on Command Line arguments question within the C Programming forums, part of the General Programming Boards category; Hello all, I have this assignement where I must write a simple copy file program passing the source file name ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    10

    Command Line arguments question

    Hello all,

    I have this assignement where I must write a simple copy file program passing the source file name as a ccommand line arguments. This is my first time working with command line argument so please bear with me.

    When calling the program, I am passing the source file name as a parameter called "source.txt" which is in the same directory as the program executable. Here is the code;

    Code:
    /*------------------------------------------------------------------------------------------*/ 
    /*  Modify the following code; take the file name as command line argument and  /* copy.                      
    /*-------------------------------------------------------------------------------------------*/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <string.h> 
    
    int main(int argc, char *argv[]){
                  
        char fileName[20];
        FILE *source,*target;
        char string[80]; 
            
        printf(" This is a File Copy program.\n");
        printf(" It copies the contents of this file to a file named copied.txt\n");
        printf(" take the input file name as command line arguments \n");
        printf(" Note: Source file should be stored in the same directory as \n");
        printf(" the executable of this program.\n\n");
    
       if (argc>1) {
          source = fopen(argv[1],"r");
    
            if (source == NULL)
            printf(" Cannot open file,  Now exiting......");
            else
            printf("\n Successful in opening file \n");
            }
             getche();
        
        
        target = fopen("copied.txt","w");
        if (target == NULL)
            printf(" Cannot open target file copied.txt . Now exiting ....\n");
            else
             printf(" Target file 'copied.txt' opened.\n"); 
            getche();
            fclose(source);
     
      /* Copy the file */ 
      
        while(fgets(string,79,source)!=NULL)
            fputs(string,target);
       
        fclose(source);
        fclose(target);
        puts(" File Copied.");
        getche();
       }
    My problem is this; when I try to read from the file and write to the target file right about here;

    Code:
     
    while(fgets(string,79,source)!=NULL)
            fputs(string,target);
    the while (fgets...) does not work at all (exits from the loops immediately) which leads me to believe that perhaps my source file is not open at all. But after my fopen above source is not equal NULL and the "Successfull" message prints.

    Now obviously I am doing something wrong. I would like to know if there is there a better way to validate wether my source file is really opened?

    Can anyone point me in the right direction? I have been reading a lot of posts on the subject and even though they did help and I believe I understand the basic principles, I am still not sure I am doing this the right way.

    Thanks in advance,

  2. #2
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Code:
            fclose(source);
     
      /* Copy the file */ 
      
        while(fgets(string,79,source)!=NULL)
            fputs(string,target);
    You close the file, and then you start reading from it?

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You do realize you're closing the source file before you even get to that point, right?

    BTW, your indention scheme could use just a little bit of work to help you see where the if/else statements are really at. In addition, when you tell the user you're quitting, you're not actually quitting.

    Edit: Late again.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Also...
    Code:
    if (argc>1) {
          source = fopen(argv[1],"r");
    
            if (source == NULL)
            printf(" Cannot open file,  Now exiting......");
    You don't actually exit, you just continue on anyway...
    Code:
    target = fopen("copied.txt","w");
        if (target == NULL)
            printf(" Cannot open target file copied.txt . Now exiting ....\n");
            else
    and again...


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    10

    Lightbulb Oops!!

    Thanks for the answers. Kicking myself in the butt right about now... can't believe I missed this..

    Thanks again

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I would also add 'b' to the fopen flags just to ensure that its open'd in binary mode...

    also
    Faster and cleaner:

    Code:
    puts(	" This is a File Copy program.\n"
    	" It copies the contents of this file to a file named copied.txt\n"
    	" take the input file name as command line arguments \n"
    	" Note: Source file should be stored in the same directory as \n"
    	" the executable of this program.\n");

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    They will run into a problem with their code if they attempt to use binary files, since they're using fputs. They'd be better off reading one character at a time, or using a fread / fwrite pair.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    10
    Noted for future reference.

    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack push order for function arguments
    By Mario F. in forum C++ Programming
    Replies: 12
    Last Post: 08-02-2007, 10:53 AM
  2. question aboout arguments
    By only1st in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2007, 09:36 PM
  3. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 02:11 PM
  4. Easy question about arguments in a function
    By pond-person in forum C++ Programming
    Replies: 5
    Last Post: 12-12-2002, 10:36 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21