Romulo Carneiro <> writes:
[...]
> //Program to get the list of command line arguments.
> #include "stdafx.h"
That's a non-standard header.
> #include "stdio.h"
stdio.h is standard, but you should use
#include <stdio.h>
> #include "process.h"
That's a non-standard header.
> int main(int argc, char* argv[])
> {
> FILE *fp;
>
> // Criando uma aplicação
> //Open a File teste.txt
> fp = fopen("teste.txt", "w");
You should check whether the fopen() call failed. It's not a big deal
in a toy program like this, but keep it in mind.
> //Print the
> fprintf( fp, "Size of Argc:%d\n", argc );
You're printing the value of argc, not its size.
C identifiers are case-sensitive; argc and Argc could be two distinct
entities. Don't develop the habit of arbitrarily capitalizing
identifiers, even for output.
> int y = MemorySize(argv);
There is no MemorySize function in standard C. I don't know what
it's supposed to do.
> //int y = sizeof(*argv);
sizeof(*arg) is simply the size in bytes of what argv points to; since
argv is a char**, sizeof(*argv) is the same as sizeof(char*). I don't
see how this information is of any use to you in this program.
> fprintf( fp, "Size of Argv: %d\n", y );
> int x = sizeof(argc);
> for (int i = 0; i<= y; i++)
> {
> fprintf(fp, "i:%d - %s\n", i, argv[i]);
> }
What you want is the *value* of argc, not it size (argc, not
sizeof(argc)).
> fclose(fp);
> system("type teste.txt");
You're assuming there's a command called "type". There may not be
such a command (or there might be a "type" command that does something
entirely different).
You could just write to stdout, but then you wouldn't also get the
"teste.txt" file. If you want your program to be portable, you can
write to "teste.txt" as you do here, then read it and write its
contents to stdout. Or you could write each line of output both to fp
and to stdout.
> return 0;
> }
Incidentally, your program uses a number of features that are specific
to C99. That's fine if you're using a compiler that supports C99, or
at least enough of it for your purposes, but be aware that it could
make your program difficult to port to other systems and
implementations.
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"