Next: Redirection and pipes Up: Input-Output Previous: File output

File input

By now you will not be surprised to know that formatted file input is done with

	fscanf(fp,...);

There is a predefined input file called stdin and fscanf(stdin,...) is the same as scanf(...). stdin can be redirected with <.

Notes

1. fscanf returns the number of items converted, as does scanf. Either will return EOF at the end of the input.

	while (scanf(...) != EOF)       /* read until CTRL-D */

	while (fscanf(...) != EOF)      /* read until end of file */

	if ( (count= fscanf("%d %d %d",&v1,&v2,&v3) ) != 3 )
		{
		/* failed to read the expected number of items */
                ...
		}

2. You can view files as streams of data read/written by these routines. Hence the apparent oddity of treating the terminal as two files.



Next: Redirection and pipes Up: Input-Output Previous: File output


maspjw@
Tue Sep 27 15:29:34 BST 1994