Please support our C advertiser: Programming Forums
![]() |
•
•
Join Date: Nov 2006
Posts: 47
Reputation:
Rep Power: 3
Solved Threads: 1
Hey guys,
in c i am trying to read an input such as a command like
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
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
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,873
Reputation:
Rep Power: 40
Solved Threads: 1014
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
If you attempt to read other strings in argv then your prog ram will most likely core dump.
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.
•
•
Join Date: Nov 2006
Posts: 47
Reputation:
Rep Power: 3
Solved Threads: 1
well wat i have at the moment is
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.
#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.
Concerning this:
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.
gets (argv);
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.
•
•
Join Date: Nov 2006
Posts: 47
Reputation:
Rep Power: 3
Solved Threads: 1
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 )
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 )
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,884
Reputation:
Rep Power: 13
Solved Threads: 197
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.
You can test it:
Hope this helps.
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.
C Syntax (Toggle Plain Text)
#include <stdlib.h> #include <stdio.h> int main( int argc, char *argv[] ) { int i; printf( "My name is: %s\n", argv[ 0 ] ); for (i = 1; i < argc; i++) printf( "arg %d: %s\n", i, argv[ i ] ); return 0; }
•
•
•
•
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
![]() |
Similar Threads
Other Threads in the C Forum
- Creating a Basic String Database (C++)
- Problem in String Search:Please Help (C)
- pointer to a string, question. (C++)
Other Threads in the C Forum
- Previous Thread: how to compute distances of certain cities using C langauage
- Next Thread: need help with single digit code
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode