-
more argv and argc
Hi all,
I am just trying to write a program that will echo the command line minus the program name... here is my code, any idea why it doesn't work? Thanks!
Code:
#include <stdio.h>
int main(int argc, char * argv [])
{
argv = argv+1;
printf(" \n");
while(**argv != NULL)
{
while (**argv != NULL)
{
putchar(**argv);
++(*argv);
}
argv = argv + 1;
}
return 0;
}
-
Good question. The first argument in main (int argc) is a counter of the number of different statements that are in the command line. Here's an example of what i'm trying to say:
c:\>my_program open whatever.txt
In this example argc will be 2 (3 arguments starting at 0). Looking for char **argv to be null can have errors when ran. So instead count argc down to 1 (0 is the path).
-
Hi,
Why you are complicating things try this one.
By
A.Sebasti...
#include<stdio.h>
int main(int argc,char * argv[])
{
int i=1;
printf("<%d>\n",argc);
while(argc!=1)
{
printf("<%s>\n",argv[i]);
i++;
argc--;
}
}
-
thanks
I didn't even think about using argc! Thanks for the help.
Clarinetster
-
Code:
int main(int argc, char * argv [])
{
int i;
for(i=1;i<argc;i++) printf("%s",argv[i]);
return 0;
}
-
Code:
#include <stdio.h>
int main (int argc, char * argv [])
{
printf ("\n");
argv++;
while (*argv != NULL)
{
//printf ("%s ", *argv);
//argv++;
// reformatted so it looks like yours...
while (**argv != "")
{
putchar (**argv);
(*argv)++;
}
argv++;
}
return 0;
}
The problem with your first code wat that is tested whether **argv was NULL, which **argv is a char, so it doesn't make sense.
-
We had a question at UNI for this ages and ages ago. We also had to print them in reverse order and test if they used it wrong. I did the part your after like Witch_King. It is easier to understand and looks neater