Home Segments Index Top Previous Next

599: Mainline

Actually, as a seasoned C programmer, you would add tests to the program to ensure that exactly one command-line argument has been supplied and to ensure that the command-line argument works as a file name. To check the number of arguments, you merely test argument_count, and exit if the number is wrong:

if (argument_count != 2) { 
  printf ("Sorry, %s requires exactly one argument.\n",  
                  argument_array [0]); 
  exit (0); 
} 

To check that the argument is a file name, you make sure that the pointer returned by fopen is not the NULL pointer, which would indicate that the file failed to open:

if (trade_source == NULL) { 
  printf ("Sorry, %s is not a file name.\n",  
                  argument_array [1]); 
  exit (0); 
}