RSS Forums RSS
Please support our C advertiser: Programming Forums
Views: 1168 | Replies: 6 | Solved | Thread Tools  Display Modes
Reply
Join Date: Nov 2006
Posts: 47
Reputation: nnobakht is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 1
nnobakht nnobakht is offline Offline
Light Poster

Pointer/String input

  #1  
Jan 16th, 2008
Hey guys,
in c i am trying to read an input such as a command like
xeyes -bg red -fg blue

i have managed to read it fine if there is no spaces so a single word such as "abc"
but when i have spaces i get segmentation error.
also since this is going into a pointer i am unsure of how i would be able to print.
(i am NOT asking for the code, just a hint to the right direction i am interested in learning the material.)
thank you
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,873
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 1014
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Pointer/String input

  #2  
Jan 16th, 2008
post your code because I can't see your monitor very well from my house argc parameter tells the number of arguments on the command line plus one for the program name itself. In your example, argc should be 5, and the valid strings in argv are
argv[0] = program name
argv[ 1] = -bg
argv[ 2] = red
argv[3] = -fg
argv[4] = blue

If you attempt to read other strings in argv then your prog ram will most likely core dump.
Last edited by Ancient Dragon : Jan 16th, 2008 at 11:37 pm.
<<Hire Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Nov 2006
Posts: 47
Reputation: nnobakht is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 1
nnobakht nnobakht is offline Offline
Light Poster

Re: Pointer/String input

  #3  
Jan 16th, 2008
well wat i have at the moment is
#include <stdio.h>

int main (int argc, char* argv[]){
printf  ("$: ");
gets (argv);
printf ("Print:\t ");
printf("%c\n", argv[0]);
return 0;
}

everything runs fine till the last printf statement.
it prints the first one so when i eneter Hello world H but after that it does not work.
Reply With Quote  
Join Date: Dec 2006
Posts: 1,633
Reputation: Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold 
Rep Power: 13
Solved Threads: 122
Aia's Avatar
Aia Aia is offline Offline
Posting Virtuoso

Re: Pointer/String input

  #4  
Jan 17th, 2008
Concerning this:
gets (argv);
NEVER, ever, ever, EVER use gets. Forget that exist.

You did not understand what Ancient Dragon was telling you.
argv is an array of pointers to strings, where input could be used at the start of the program in command line.
If you want to obtain input from user after the program is running, use the function fgets() to read it into an array of chars. Then separate the words, flags or commands entered by reading up to the space for each one.
Last edited by Aia : Jan 17th, 2008 at 12:15 am.
At the very moment that I find myself in the side of the mayority, I will know that I need to re-think my ideas. ~ In my book.
Reply With Quote  
Join Date: Nov 2006
Posts: 47
Reputation: nnobakht is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 1
nnobakht nnobakht is offline Offline
Light Poster

Re: Pointer/String input

  #5  
Jan 17th, 2008
k thats actually wat i am trying to do with the argv but im fairly sure my main problem is that i dont understand how to actually use it i understand the concept of argv.
one question is that the argc does it automatically get the length?
would you please be able to give me a rough example of the usage
wat im trying to do is that i have a function type_prompt that jsut prints $: and than i am trying to get a user input of a command to pass on to another function to basically create another process.
(thanks to everyone for trying to help i have never programmed in C )
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,884
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 197
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Pointer/String input

  #6  
Jan 17th, 2008
No, you've completely missed the concept of argv.

argv is an array of strings (where a string is a pointer to char). It is not safe to change argv.
argc is the number of items in the argv array.

argv[0] is the program name.
argv[1] is the first argument.
etc.

This might help you.
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. int main( int argc, char *argv[] ) {
  5. int i;
  6.  
  7. printf( "My name is: %s\n", argv[ 0 ] );
  8.  
  9. for (i = 1; i < argc; i++)
  10. printf( "arg %d: %s\n", i, argv[ i ] );
  11.  
  12. return 0;
  13. }
You can test it:
D:\prog> a
My name is: a

D:\prog> a Hello world!
My name is: a
arg 1: Hello
arg 2: world!

D:\prog> ren a.exe carlos.exe

D:\prog> Carlos de la Paz Gutieres-Hernandez
My name is: Carlos
arg 1: de
arg 2: la
arg 3: Paz
arg 4: Gutieres-Hernandez
Hope this helps.
Reply With Quote  
Join Date: Nov 2006
Posts: 47
Reputation: nnobakht is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 1
nnobakht nnobakht is offline Offline
Light Poster

Re: Pointer/String input

  #7  
Jan 17th, 2008
hey
thanks soooo much for the example it makes much more sense now i will try and implement this.
thanks to everyone for helping out.
cheers
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 11:27 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC