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

Recommended Answers

All 6 Replies

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.

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.

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.

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 )

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.

#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;
  }

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.