Why can't I get the argv[1] value?

This is a discussion on Why can't I get the argv[1] value? within the C Programming forums, part of the General Programming Boards category; Why won't this let the user type in the argv[1] value? Code: #include <stdio.h> int main ( int argc, char ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    100

    Question Why can't I get the argv[1] value?

    Why won't this let the user type in the argv[1] value?


    Code:
    #include <stdio.h>
    
    int main ( int argc, char *argv[] )
    {
    	gets(argv[1]);
        if ( argc != 2 ) /* argc should be 2 for correct execution */
        {
            /* We print argv[0] assuming it is the program name */
            printf( "usage: %s filename", argv[0] );
        }
        else 
        {
            // We assume argv[1] is a filename to open
            FILE *file = fopen( argv[1], "r" );
    
            /* fopen returns 0, the NULL pointer, on failure */
            if ( file == 0 )
            {
                printf( "Could not open file\n" );
            }
            else 
            {
                unsigned char x;
                /* read one character at a time from file, stopping at EOF, which
                   indicates the end of the file.  Note that the idiom of "assign
                   to a variable, check the value" used below works because
                   the assignment statement evaluates to the value assigned. */
                while  ( ( x = fgetc( file ) ) != EOF )
                {
                    printf( "%c", x );
                }
            }
            fclose( file );
        }
    return 0;
    }
    Yes, I pulled much of this from the tutorial, but I don't know how to get a program to LET a user PUT IN command line arguments. I'm just trying to learn how to program in C. Thanks!

  2. #2
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    Quote Originally Posted by jsrig88
    ...but I don't know how to get a program to LET a user PUT IN command line arguments. I'm just trying to learn how to program in C. Thanks!
    Have you ever used command line? For example in DOS what does "dir /w" mean or in any UNIX system what does "ls -l" mean? Those options used after command name are the command line arguments and passed to your program by operating system (actually by the OS shell).

    You don't need an extra gets() (which is unsafe!, use fgets() instead). You can find lots of exmaples about "how to pass parameter to main". I am sure that oyur C book has such a section!

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    100

    Clarifying the situation

    YES, I have used the command line before. And yes, I have used other, more C++-related methodologies for obtaining command line arguments from the user. And no, I do NOT have a C book.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    Quote Originally Posted by jsrig88
    And no, I do NOT have a C book.
    says you

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >gets(argv[1]);
    That's about the coolest thing I've seen someone try to do...this week at least.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    100

    argument

    "Says you"!? I've been using the tutorials on this website. I've also learned some C++ from a C++ book. But I need to do stuff that is not involved with C++. I want to know the C way of doing it.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I want to know the C way of doing it.
    The C way of doing it is identical to the C++ way of doing it. Your approach is largely correct, with the difference that you don't seem to be passing your arguments from the command line itself and expecting to somehow prompt the user from within your program. Here's a quick example. You get command line arguments from the program's startup code, not from any code you write. In other words, the system calls main and passes the command line arguments:
    Code:
    #include <stdio.h>
    
    int main ( int argc, char *argv[] )
    {
      int i;
    
      for ( i = 0; i < argc; i++ )
        printf ( "argv[%d] = %s\n", i, argv[i] );
    
      return 0;
    }
    Then on the command line, you invoke the program like so:
    Code:
    $ myprog This is a test
    And the output would be something like:
    Code:
    argv[0] = myprog
    argv[1] = This
    argv[2] = is
    argv[3] = a
    argv[4] = test
    My best code is written with the delete key.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I think most users get lost at the phrase "command line", having never encountered one before.


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

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I think most users get lost at the phrase "command line", having never encountered one before.
    That's probably true. It's scary how the most common question when talking about command line parameters is "where do I click?".
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    100

    what else do I need?

    Thanks, prelude. But I put that code in, and when I build it and debug it in MS Visual Studio .NET 2003, it still won't let the user put in anything. Is there something up with the compiler in that case, or is there another piece of code I need to put in?

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by jsrig88
    But I put that code in, and when I build it and debug it in MS Visual Studio .NET 2003, it still won't let the user put in anything. Is there something up with the compiler in that case, or is there another piece of code I need to put in?
    Run it from the command line. Or find where to tell the debugger what the command line arguments should be. For example, MSVC6 had this:
    Attached Images Attached Images  
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. main() ; argv[1] = string at argv[0]???
    By UCnLA in forum C Programming
    Replies: 1
    Last Post: 03-31-2008, 01:16 AM
  2. passing argv[1] to a method
    By Beowolf in forum C++ Programming
    Replies: 15
    Last Post: 09-23-2007, 05:14 PM
  3. Replies: 5
    Last Post: 08-14-2007, 11:34 AM
  4. Some Socket Problems...?
    By Tal0n in forum Linux Programming
    Replies: 1
    Last Post: 04-01-2004, 05:31 AM
  5. gethostbyaddr( )
    By xlordt in forum Networking/Device Communication
    Replies: 4
    Last Post: 10-06-2003, 09:25 PM

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