Command Line arguments in C

This is a discussion on Command Line arguments in C within the C Programming forums, part of the General Programming Boards category; Hello, I wrote a program in C that does the following: user enters any number of arguments in command line ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    3

    Command Line arguments in C

    Hello,

    I wrote a program in C that does the following: user enters any number of arguments in command line separated by space(single or multiple) and the program outputs http://www.[user arguments].com. (Trying to emulate what mozilla browser does. enter web address and press cntrl+enter, it will add webaddress.com for you).

    My program achieves somewhat same except one thing. It removes the spaces between arguments. I would like to echo the command line arguments. How can I do that? How can I read the spaces between two arguments? I know you can add double quote to read them as a one argument but I don't want do that.

    Thank you!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If they enter more than one argument, then your program will get more than one argument (in argv[1], argv[2], etc.) argc tells you how many there are.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    3
    yes. i am aware of that.

    e.g. program.exe angel1 learning

    output -> http://www.angel1learning.com

    what I want to achieve is this : http://www.angel1 learning. com (notice the space between angel1 and learning)

    echo the command line argument with http:/www and .com added at front and end without adding double quotes(e.g. program.exe "angel1 learning" -> with this i can get http://www.angel1 learning.com)
    Last edited by hexabina; 05-24-2010 at 12:47 PM.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    You can't. The operating system parses it and unless the OS has some interface to retrieve it (I think windows does), it's not possible to find out. The user would have to be smart enough to put it in quotes to show the OS it's only one argument; as it is in this case.
    Eg. ./program "some website"

    You could string all the arguments together and put a single space in between them. That will work, unless a user types two spaces after each other or a tab in stead of a space.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    3
    thanks! i was thinking along that line. Just curious, how does echo command in MS-DOS do it? Did some googling but didn't find much.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by hexabina View Post
    thanks! i was thinking along that line. Just curious, how does echo command in MS-DOS do it? Did some googling but didn't find much.
    It either can't (ie. doing it the same as I suggested, adding one space between each argument), or it's not a program but part of the shell. In Linux it's the former, in Windows I don't know. Or it uses some windows api I don't know.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  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, 01:11 PM

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