passing command line arguments

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

passing command line arguments

 
0
  #1
Oct 15th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2003
Posts: 46
Reputation: Dante Shamest is an unknown quantity at this point 
Solved Threads: 0
Dante Shamest's Avatar
Dante Shamest Dante Shamest is offline Offline
Light Poster

Re: passing command line arguments

 
0
  #2
Oct 15th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: passing command line arguments

 
0
  #3
Oct 15th, 2005
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 7134 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC