|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
In a previous post I mentioned a beautifully working simple function
called out with this prototype: int out (int n,char *buffer); Great. But how would I handle the command line? I have arrays and sub-arrays char *argv[] and argc. int main (int argc, char *argv[]) { /* In one of these two parameters I am going to have to take a string and send it back. What I want is a perfect version of puts. Which of these two parameters would take a single string like "hello world\n" and print it adding newline like puts ? I'm going to need extra functions. Maybe atoi and others. */ return puts(string); /* from argc or argv[] */ Am I making sense ? Bill |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Bill Cunningham wrote:
> In a previous post I mentioned a beautifully working simple function > called out with this prototype: > > int out (int n,char *buffer); Nice prototype. What sort of data do 'n' and 'buffer' represent? > Great. But how would I handle the command line? I have arrays and > sub-arrays char *argv[] and argc. > > int main (int argc, char *argv[]) { > /* In one of these two parameters I am going to have to take a > string Only one of those parameters is capable of presenting a "string". > and send it back. Back where? > What I want is a perfect version of puts. Which > of these two parameters would take a single string like "hello world\n" > and print it adding newline like puts ? I'm going to need extra functions. > Maybe atoi and others. */ > > return puts(string); /* from argc or argv[] */ > > Am I making sense ? No and yes. You want one of the occurrences of argv[]. /Which/ occurrence depends on a) the order of argument data as provided by the environment (the "command line" or it's equivalent, and your "usage" for your program), and b) the value of argc argc will contain a count of the number of valid argv[] elements, or 0 if argc > 0 argv[0] will be a pointer to a string containing the "program name", whatever /that/ is argv[1] through argv[argc-1] will be pointers to strings, each string being provided by your execution environment. Typically, argv[1] is the first string provided on the commandline, argv[2] is the 2nd, etc. argv[argc] will be NULL For example, using the code below... #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { printf("argc == %d\n",argc); if (argc == 0) puts("Execution environment does not support argument strings"); else { int arg; for (arg = 0; arg <= argc; ++arg) { printf("argv[%d] = ",arg); if (argv[arg] == NULL) puts("NULL"); else printf("\"%s\"\n",argv[arg]); } } return EXIT_SUCCESS; } In my execution environment, we see that ~/code/tmp $ args argc == 1 argv[0] = "args" argv[1] = NULL And that ~/code/tmp $ args a b c "d E fgh" i jkl argc == 7 argv[0] = "args" argv[1] = "a" argv[2] = "b" argv[3] = "c" argv[4] = "d E fgh" argv[5] = "i" argv[6] = "jkl" argv[7] = NULL -- Lew Pitcher Master Codewright & JOAT-in-training | Registered Linux User #112576 http://pitcher.digitalfreehold.ca/ | GPG public key available by request ---------- Slackware - Because I know what I'm doing. ------ |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
> Nice prototype. What sort of data do 'n' and 'buffer' represent?
> >> Great. But how would I handle the command line? I have arrays and >> sub-arrays char *argv[] and argc. >> >> int main (int argc, char *argv[]) { >> /* In one of these two parameters I am going to have to take a >> string > > Only one of those parameters is capable of presenting a "string". > >> and send it back. > > Back where? > > >> What I want is a perfect version of puts. Which >> of these two parameters would take a single string like "hello world\n" >> and print it adding newline like puts ? I'm going to need extra >> functions. >> Maybe atoi and others. */ >> >> return puts(string); /* from argc or argv[] */ >> >> Am I making sense ? > > No and yes. > > You want one of the occurrences of argv[]. /Which/ occurrence depends on > a) the order of argument data as provided by the environment (the "command > line" or it's equivalent, and your "usage" for your program), and > b) the value of argc > > argc will contain a count of the number of valid argv[] elements, or 0 > > if argc > 0 > argv[0] will be a pointer to a string containing the "program name", > whatever /that/ is > argv[1] through argv[argc-1] will be pointers to strings, each string > being provided by your execution environment. Typically, argv[1] is > the first string provided on the commandline, argv[2] is the 2nd, etc. > argv[argc] will be NULL > > For example, using the code below... > > #include <stdio.h> > #include <stdlib.h> > > int main(int argc, char *argv[]) > { > printf("argc == %d\n",argc); > > if (argc == 0) > puts("Execution environment does not support argument strings"); > else > { > int arg; > > for (arg = 0; arg <= argc; ++arg) > { > printf("argv[%d] = ",arg); > if (argv[arg] == NULL) > puts("NULL"); > else > printf("\"%s\"\n",argv[arg]); > } > } > > return EXIT_SUCCESS; > } > > In my execution environment, we see that > > ~/code/tmp $ args > argc == 1 > argv[0] = "args" > argv[1] = NULL > > > And that > > ~/code/tmp $ args a b c "d E fgh" i jkl > argc == 7 > argv[0] = "args" > argv[1] = "a" > argv[2] = "b" > argv[3] = "c" > argv[4] = "d E fgh" > argv[5] = "i" > argv[6] = "jkl" > argv[7] = NULL > > Lew alot of your code went over my head. Let me write a simple function of what I want. int out(char * buffer) { return puts(buffer); } Out just behaves as puts. Nice function. But I want to call from command line in linux enviornment. out "hello world" /*< stdin*/ /*stdout>*/ hello world /*with \n from puts */ So I know I will have to begin like this. int main (int argc,char **argv) { puts will have to be in the body somewhere and argc and argv control somewhere. Also since main returns int I think I would have to convert the char* into an int. It's what to do with argc and argv[] that I don't know what to do. Bill |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
> Lew alot of your code went over my head. Let me write a simple function
> of what I want. > Ok you're demonstrating how argc and argv works. Bill |
|
![]() |
| Outils de la discussion | |
|
|