Back to DFS's C Page


Command Line Basics

Most programs that you run from a prompt in an xterm window allow you to specify extra information that the programs can use. This short page illustrates how you could get filenames the way cp and mv do.

Other programs which allow the user to specify data used by the program have been termed interactive: they prompt the user for information and then proceed to use it. An example of this is the interactive triangle-area program, which uses the printf()/scanf() combination to get information.

This raises two issues: How does a program obtain information from the command line? How does the user know how to use the program?

Sample code is provided.

Getting Access to the User's Arguments

The mechanism for accessing the arguments is quite simple. Consider the following command line which could be used to get the program filecopy1 to make a copy called xxx.c of the file prog.c.

filecopy1 prog.c xxx.c

The system counts the program name and all items terminated by whitespace (in this case for a total of three) and sends this value to main() as the first argument. It also creates an array of null-terminated strings and passes a pointer to the zeroth as the second argument.

The definition of main() without the body of the source takes the following form.

void main(argc, argv)
int argc;	/* the number of arguments */
char *argv[];  /* array of pointers to the argument strings */
{
} 

argv[0] points to the name of the program; argv[1] points to the string prog.c; and argv[2] points to the string xxx.c. These pointers are then available so that the strings can be accessed inside main().

Back to Top

Letting the User Know How to Use the Program

Since the user receives no explicit prompts, the program must make decisions given the information provided from the command line. Consider the following if structure:

   if( (argc < 3) ||
     (strncmp(argv[1], "--help", 6) == 0) )
   {
      printf("Usage: filecopy1 SOURCE DEST\n");
      exit(1);
   }

If the program is provided with fewer than three arguments, it cannot proceed. It is also traditional that if the first argument after the program name is --help, the program responds with a line of output explaining how to use the program.

Back to Top

Abbreviated Sample Code to Copy a File

The following complete program demonstrates how to access and process the command line information. The purpose of the program is to create a copy of a file. N.B. There is no bulletproofing included.

/* Copy a file */
/* filecopy1.c */
#include <stdio.h>

void main(argc, argv)
int argc;	/* the number of arguments */
char *argv[];  /* array of pointers to the argument strings */
{
   int ch; /* input character */
   char inputfile[80], outputfile[80];
   FILE *fpin, *fpout; /* input & output struct pointers */

   if( (argc == 1) ||
     (strncmp(argv[1], "--help", 6) == 0) )
   {
      printf("Usage: filecopy SOURCE DEST\n");
      exit(1);
   }
   /* get the filenames specified by the user */
   strcpy(inputfile, argv[1]);
   strcpy(outputfile, argv[2]);

   /* open the files */
   fpin = fopen(inputfile, "r");
   fpout = fopen(outputfile, "w");

   /* copy the file char by char */
   while( (ch = fgetc(fpin)) != EOF)
   {
      fputc(ch, fpout);
   }

   /* close the files */
   fclose(fpin);
   fclose(fpout);
}

Back to Top


© 1998-99 DFStermole
Created 17 Nov 98
Modified 29 Dec 99