-
One useful technique when writing a program is to allow the user to
specify arguments on the command line, for example the name of a file
to be read, or values to control the execution of the program. Although
the main program is often defined with no arguments, you can always
use two optional arguments to main to get access to any
information which was provided on the command line, namely argc,
which is an integer containing the number of arguments passed on the command
line (including the name of the program itself), and argv, which is an
array of pointers to the (null-terminated)
values of the arguments, with the convention that
argv[0] points to the name of the program.
Memory for the values is automatically
provided by the operating system, and is guaranteed to be stable throughout
the execution of the program.
The following program shows how to declare the necessary
arguments, as well as how to access the values of command line arguments
passed to the program.
main(int argc, char *argv[])
{
short i;
printf("There were %d arguments passed to %s\n", argc, argv[0]);
printf("The arguments were:\n");
for(i=1;i<argc;i++)printf("Argument %d: %s\n",i,argv[i]);
}
Note that arguments passed to programs are always null-terminated character
strings, and should be converted (using atoi, atof or
sscanf) if a numeric value is desired. For an example of how to parse
UNIX style command line options (with hyphens), see
~s243/samples/sq.c or the man page
for getopt.
-
Virtually all the I/O that you need when dealing with numerical operations
will be formatted I/O, using fprintf, fscanf, and related
functions. However, it is occasionally useful to be able to read input
as simply characters, and then process it within your program, or to be
able to construct a line of text as a character string, and then write
it to a file. To perform these kinds of tasks, you can open a file using
fopen, and then pass the resulting FILE pointer to one of
the following routines:
To read or write one line at a time (where a line is defined by the presence
of a newline character):
fgets(char *line, int maxlen, FILE *fp);
fputs(char *line, FILE *fp);
To read or write one character at a time:
char getc(FILE *fp); /* returns the next character from a file */
putc(char c, FILE *fp);
These routines are declared in stdio.h, so that file should be
#included in any program which calls them.
-
There are even lower level I/O routines available which simply transfer
the internal representations of objects to a file, or transfer bytes of
information from a file to a specified location in the computer's memory.
For example, you could scan in a set of formatted numbers, and then
write their internal binary representations to a file, which could very
quickly and efficiently be read back into memory at a later time. (This
is essentially the way virtual memory works.)
You can pass a FILE
pointer from fopen to the fread or fwrite function
s to
acheive these goals.
The prototypes for these functions are as follows:
int fread(char *from, int size, int nitems, FILE *fp);
int fwrite(char *to, int size, int nitems, FILE *fp);
The fread and fwrite functions return the number of items
actually read. They are declared in <stdio.h>.
-
Although C has a reputation for not providing much support for character
strings, there are a few handy functions available as part of the standard
library. Most of these routines expect that strings will be null terminated,
so make sure you allow enough space to include the null character at the
end of the string. When your string is not null-terminated, or you only
want to manipulate a substring, use the version of the function with an
`` n'' in its name.
int strlen(char *s); /* returns the length of a string */
int strcpy(char *to, char *from); /* imitates to = from */
int strncpy(char *to, char *from, int n);
char *index(char *s,char c) /* ptr to first c in s */
int strcmp(char *s1, char *s2); /* <0 if s1<s2, 0 if s1==s2, >0 if s1>s2 */
int strncmp(char *s1, char *s2, int n);
There are quite a few other string handling functions; type man string
for more information.