Why won't this let the user type in the argv[1] value?
Code:
#include <stdio.h>
int main ( int argc, char *argv[] )
{
gets(argv[1]);
if ( argc != 2 ) /* argc should be 2 for correct execution */
{
/* We print argv[0] assuming it is the program name */
printf( "usage: %s filename", argv[0] );
}
else
{
// We assume argv[1] is a filename to open
FILE *file = fopen( argv[1], "r" );
/* fopen returns 0, the NULL pointer, on failure */
if ( file == 0 )
{
printf( "Could not open file\n" );
}
else
{
unsigned char x;
/* read one character at a time from file, stopping at EOF, which
indicates the end of the file. Note that the idiom of "assign
to a variable, check the value" used below works because
the assignment statement evaluates to the value assigned. */
while ( ( x = fgetc( file ) ) != EOF )
{
printf( "%c", x );
}
}
fclose( file );
}
return 0;
}
Yes, I pulled much of this from the tutorial, but I don't know how to get a program to LET a user PUT IN command line arguments. I'm just trying to learn how to program in C. Thanks!