Thread: comand line argument

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    82

    comand line argument

    hey,
    Is it a must to store file names in argv so that you can view them? Or the program does it automatically even when file names are stored in another variable?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can use some other name instead of argv, but generally it is better to stick to convention.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    82
    Quote Originally Posted by laserlight View Post
    You can use some other name instead of argv, but generally it is better to stick to convention.
    Actually what I mean is, for example:
    Code:
    #include<stdio.h>
    
    main(int argc, char *argv[])
    {
             FILE  *fptr;
             
             fptr = fopen("somefile.dat","wb");
    }
    will this file name be stored in argv?
    Sorry for not being clear before.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Harith View Post
    will this file name be stored in argv?
    No. Why would you expect it to?

    Your code is passing a string to fopen(). That string is unrelated to argv. fopen() does not affect argv.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    82
    Quote Originally Posted by grumpy View Post
    No. Why would you expect it to?

    Your code is passing a string to fopen(). That string is unrelated to argv. fopen() does not affect argv.
    I just thought that when you open a file you are passing an argument to main. So why is this command line argument useful?

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Harith View Post
    I just thought that when you open a file you are passing an argument to main.
    Nope. Opening files has nothing to do with passing an argument to main.

    So why is this command line argument useful?
    So that you can pass arguments to the program from the command line, but the arguments don't have to be file names.

    Just keep learning the language and you'll come to understand this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Sep 2013
    Posts
    82
    Quote Originally Posted by oogabooga View Post
    Nope. Opening files has nothing to do with passing an argument to main.


    So that you can pass arguments to the program from the command line, but the arguments don't have to be file names.

    Just keep learning the language and you'll come to understand this.
    Okay, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. command line argument help
    By jwroblewski44 in forum C Programming
    Replies: 10
    Last Post: 05-10-2012, 09:55 PM
  2. use of command line argument
    By natrajdreams in forum C Programming
    Replies: 2
    Last Post: 04-19-2009, 11:07 AM
  3. command line argument
    By csit in forum C Programming
    Replies: 4
    Last Post: 09-19-2007, 11:35 AM
  4. proxy comand line in c
    By enlinux in forum Linux Programming
    Replies: 2
    Last Post: 07-18-2003, 11:21 PM
  5. '*' as a command line argument
    By kooma in forum C Programming
    Replies: 6
    Last Post: 02-26-2002, 02:18 PM