How 'main(int argc, char ** argv)' is implemented?

W

william

I wonder how 'main(int argc, char ** argv)' is implemented? How
does it get
the string literals separated by whitespace from the stdin stream?

And is there any difference between 'char**' and 'char *[ ]'(the
second argument of function main)?

My initial purpose was to parse the input from the keyboard like
'main' does, code is here:
#include <stdio.h>
#include <string.h>
.......(omitted)

char input[256];
char * token;
printf("/> ");
scanf("%s", input);

token=strtok(input, ' '); //use whitespace as delimiter;
^using this line, I intent to parse the input from keyboard into
different tokens(which will be arguments for my further use)

****************
However, the problem was, I found 'scanf' can only get the first
string ending with '\0'. For example, if I type "command arg1 arg2
arg3" following the hint "/> " that I printed, scanf can only get
command into the array 'input', and all arg1, arg2... are truncated.
And thus my purpose can not be reached by token=strtok(input, ' ')
repeatedly, because input has only 'command\0'.

Can anyone tell me how I can implement this: get and parse the user
input conveniently? Thank you
 
R

Richard Tobin

william said:
I wonder how 'main(int argc, char ** argv)' is implemented? How
does it get
the string literals separated by whitespace from the stdin stream?

The have nothing to do with stdin. Typically they are arguments
passed on the command line and broken up by some part of the operating
system - in unix, it's the shell.
And is there any difference between 'char**' and 'char *[ ]'(the
second argument of function main)?

No, these both mean the same thing when used to declare the arguments
to functions.
My initial purpose was to parse the input from the keyboard like
[...]

It's unlikely that the mechanism used for splitting command line
arguments will help you with this.

-- Richard
 
M

mark_bluemel

I wonder how 'main(int argc, char ** argv)' is implemented? How
does it get
the string literals separated by whitespace from the stdin stream?

That's a job for the host environment, for example the unix shell. You
could look at some shell source, but whether it would help is
questionable...
And is there any difference between 'char**' and 'char *[ ]'(the
second argument of function main)?
No.

My initial purpose was to parse the input from the keyboard like
'main' does, code is here:
#include <stdio.h>
#include <string.h>
......(omitted)

char input[256];
char * token;
printf("/> ");
scanf("%s", input);

token=strtok(input, ' '); //use whitespace as delimiter;
^using this line, I intent to parse the input from keyboard into
different tokens(which will be arguments for my further use)

****************
However, the problem was, I found 'scanf' can only get the first
string ending with '\0'. For example, if I type "command arg1 arg2
arg3" following the hint "/> " that I printed, scanf can only get
command into the array 'input', and all arg1, arg2... are truncated.
And thus my purpose can not be reached by token=strtok(input, ' ')
repeatedly, because input has only 'command\0'.

Can anyone tell me how I can implement this: get and parse the user
input conveniently? Thank you

What do you believe scanf("%s",input) will do with the data on the
standard input stream?
[Hint: it's generally not what you want...] This is probably in the
FAQs somewhere...

If you just want to read a line from stdin, why not use a sensible
function, like fgets() or one of the alternatives (e.g. ggets()) which
are posted here from time to time.
 
W

william

I wonder how 'main(int argc, char ** argv)' is implemented? How
does it get
the string literals separated by whitespace from the stdin stream?

That's a job for the host environment, for example the unix shell. You
could look at some shell source, but whether it would help is
questionable...
And is there any difference between 'char**' and 'char *[ ]'(the
second argument of function main)?
No.



My initial purpose was to parse the input from the keyboard like
'main' does, code is here:
#include <stdio.h>
#include <string.h>
......(omitted)
char input[256];
char * token;
printf("/> ");
scanf("%s", input);
token=strtok(input, ' '); //use whitespace as delimiter;
^using this line, I intent to parse the input from keyboard into
different tokens(which will be arguments for my further use)
****************
However, the problem was, I found 'scanf' can only get the first
string ending with '\0'. For example, if I type "command arg1 arg2
arg3" following the hint "/> " that I printed, scanf can only get
command into the array 'input', and all arg1, arg2... are truncated.
And thus my purpose can not be reached by token=strtok(input, ' ')
repeatedly, because input has only 'command\0'.
Can anyone tell me how I can implement this: get and parse the user
input conveniently? Thank you

What do you believe scanf("%s",input) will do with the data on the
standard input stream?
[Hint: it's generally not what you want...] This is probably in the
FAQs somewhere...

If you just want to read a line from stdin, why not use a sensible
function, like fgets() or one of the alternatives (e.g. ggets()) which
are posted here from time to time.

Thank you for the replies!
 
W

william

That's a job for the host environment, for example the unix shell. You
could look at some shell source, but whether it would help is
questionable...
And is there any difference between 'char**' and 'char *[ ]'(the
second argument of function main)?
My initial purpose was to parse the input from the keyboard like
'main' does, code is here:
#include <stdio.h>
#include <string.h>
......(omitted)
char input[256];
char * token;
printf("/> ");
scanf("%s", input);
token=strtok(input, ' '); //use whitespace as delimiter;
^using this line, I intent to parse the input from keyboard into
different tokens(which will be arguments for my further use)
****************
However, the problem was, I found 'scanf' can only get the first
string ending with '\0'. For example, if I type "command arg1 arg2
arg3" following the hint "/> " that I printed, scanf can only get
command into the array 'input', and all arg1, arg2... are truncated.
And thus my purpose can not be reached by token=strtok(input, ' ')
repeatedly, because input has only 'command\0'.
Can anyone tell me how I can implement this: get and parse the user
input conveniently? Thank you
What do you believe scanf("%s",input) will do with the data on the
standard input stream?
[Hint: it's generally not what you want...] This is probably in the
FAQs somewhere...
If you just want to read a line from stdin, why not use a sensible
function, like fgets() or one of the alternatives (e.g. ggets()) which
are posted here from time to time.

Thank you for the replies!

I found a solution:

using fread:
....
fread(destination_array, 1, sizeOfArray, stdin);
....
Then I process this array, and everytime I got a whitespace=20(ascii
value), I append '\0' at the end and use it as an argument.

Is there any better way to do the same thing? Thank you
 
W

william

On Mar 19, 12:29 pm, (e-mail address removed) wrote:
I wonder how 'main(int argc, char ** argv)' is implemented? How
does it get
the string literals separated by whitespace from the stdin stream?
That's a job for the host environment, for example the unix shell. You
could look at some shell source, but whether it would help is
questionable...
And is there any difference between 'char**' and 'char *[ ]'(the
second argument of function main)?
No.
My initial purpose was to parse the input from the keyboard like
'main' does, code is here:
#include <stdio.h>
#include <string.h>
......(omitted)
char input[256];
char * token;
printf("/> ");
scanf("%s", input);
token=strtok(input, ' '); //use whitespace as delimiter;
^using this line, I intent to parse the input from keyboard into
different tokens(which will be arguments for my further use)
****************
However, the problem was, I found 'scanf' can only get the first
string ending with '\0'. For example, if I type "command arg1 arg2
arg3" following the hint "/> " that I printed, scanf can only get
command into the array 'input', and all arg1, arg2... are truncated.
And thus my purpose can not be reached by token=strtok(input, ' ')
repeatedly, because input has only 'command\0'.
Can anyone tell me how I can implement this: get and parse the user
input conveniently? Thank you
What do you believe scanf("%s",input) will do with the data on the
standard input stream?
[Hint: it's generally not what you want...] This is probably in the
FAQs somewhere...
If you just want to read a line from stdin, why not use a sensible
function, like fgets() or one of the alternatives (e.g. ggets()) which
are posted here from time to time.
Thank you for the replies!

I found a solution:

using fread:
...
fread(destination_array, 1, sizeOfArray, stdin);

No, here it still doesn't work. because I have to input exactly
'sizeOfArray' characters to finish this call, which is not the case I
want to simulate.
 
M

mark_bluemel

On Mar 19, 12:29 pm, (e-mail address removed) wrote:
I wonder how 'main(int argc, char ** argv)' is implemented? How
does it get
the string literals separated by whitespace from the stdin stream?
That's a job for the host environment, for example the unix shell. You
could look at some shell source, but whether it would help is
questionable...
And is there any difference between 'char**' and 'char *[ ]'(the
second argument of function main)?
No.
My initial purpose was to parse the input from the keyboard like
'main' does, code is here:
#include <stdio.h>
#include <string.h>
......(omitted)
char input[256];
char * token;
printf("/> ");
scanf("%s", input);
token=strtok(input, ' '); //use whitespace as delimiter;
^using this line, I intent to parse the input from keyboard into
different tokens(which will be arguments for my further use)
****************
However, the problem was, I found 'scanf' can only get the first
string ending with '\0'. For example, if I type "command arg1 arg2
arg3" following the hint "/> " that I printed, scanf can only get
command into the array 'input', and all arg1, arg2... are truncated.
And thus my purpose can not be reached by token=strtok(input, ' ')
repeatedly, because input has only 'command\0'.
Can anyone tell me how I can implement this: get and parse the user
input conveniently? Thank you
What do you believe scanf("%s",input) will do with the data on the
standard input stream?
[Hint: it's generally not what you want...] This is probably in the
FAQs somewhere...
If you just want to read a line from stdin, why not use a sensible
function, like fgets() or one of the alternatives (e.g. ggets()) which
are posted here from time to time.
Thank you for the replies!
I found a solution:
using fread:
...
fread(destination_array, 1, sizeOfArray, stdin);

No, here it still doesn't work. because I have to input exactly
'sizeOfArray' characters to finish this call, which is not the case I
want to simulate.

If you want to read a line of input (and I think you probably do),
then fgets() is the best bet, if you only want to use standard library
routines.

Once you've got your string, you could use strtok() (with care).
 
N

Nick Keighley

william wrote:

My initial purpose was to parse the input from the keyboard

#include <stdio.h>
#include <string.h>
......(omitted)

char input[256];
char * token;
printf("/> ");
scanf("%s", input);

token=strtok(input, ' '); //use whitespace as delimiter;
^using this line, I intent to parse the input from keyboard into
different tokens(which will be arguments for my further use)

****************
However, the problem was, I found 'scanf' can only get the first
string ending with '\0'. For example, if I type "command arg1 arg2
arg3" following the hint "/> " that I printed, scanf can only get
command into the array 'input', and all arg1, arg2... are truncated.
And thus my purpose can not be reached by token=strtok(input, ' ')
repeatedly, because input has only 'command\0'.

Can anyone tell me how I can implement this: get and parse the user
input conveniently?

try fgets() it reads whole lines.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top