944,126 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 15858
  • C RSS
Oct 15th, 2005
0

passing command line arguments

Expand Post »
I'm trying to pass arguments to my program so that I can store them into an array. For some reason my IDE/compiler is treating the blank space in between arguments as an argument. I'm using bloodshed, but it doesn't say anything about a seperation character. Can anyone tell me what I'm doing wrong?

I'm doing something like this:
Bob Bill
that should be two parameters, but for some reason it returns three.
Similar Threads
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Oct 15th, 2005
0

Re: passing command line arguments

It appears to work for me. The version of my Dev-C++ IDE is 4.9.9.2. One should note that the first parameter is always the name of your program.

This is the program I used for testing.

  1. #include <stdio.h>
  2.  
  3. int main(int argc, char *argv[])
  4. {
  5. if ( argc >= 3 )
  6. {
  7. printf( "arg[1] = %s\n", argv[1] );
  8. printf( "arg[2] = %s", argv[2] );
  9. }
  10.  
  11. getchar();
  12. return 0;
  13. }

This picture shows how I passed the parameters.



And this is my output.

  1. arg[1] = Bob
  2. arg[2] = Bill
Reputation Points: 13
Solved Threads: 0
Light Poster
Dante Shamest is offline Offline
46 posts
since Apr 2003
Oct 15th, 2005
0

Re: passing command line arguments

Quote originally posted by server_crash ...
I'm trying to pass arguments to my program so that I can store them into an array. For some reason my IDE/compiler is treating the blank space in between arguments as an argument.
There is nothing wrong with your compiler or IDE -- that is the customary way C program startup code uses to parse and build argv strings. If you have a parameter that y0u want to include spaces, then you have to surround it with double quotes
  1. c:> myprogram "This is one parameter" <Enter>

in the above, argc will be 2, argv[0] == the name of the program and argv[1] == "This is one parameter". Without the quotes, argc == 5 and there will be 6 strings in argv.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is online now Online
21,961 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Counting occurences in C
Next Thread in C Forum Timeline: Today Question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC