PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > lost.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
lost.

Réponse
 
LinkBack Outils de la discussion
Vieux 10/04/2008, 23h18   #1
Bill Cunningham
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut lost.

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


  Réponse avec citation
Vieux 11/04/2008, 00h01   #2
Lew Pitcher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: lost.

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. ------


  Réponse avec citation
Vieux 11/04/2008, 02h27   #3
Bill Cunningham
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: lost.

> 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




  Réponse avec citation
Vieux 11/04/2008, 02h33   #4
Bill Cunningham
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: lost.

> 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


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h39.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14394 seconds with 12 queries