1 2 3 4 5 6

Example
FILE* fp;FILE* fptr; scanf ("%d", &item);
fscanf (stdin, "%d", &item);

fp = fopen ("tally.data", "r"); fscanf (fp, "%d", &item);

printf ("%d", item);
fprintf (stdout, "%d", item);

/* as above*/

fptr = fopen ("tally.out", "w");
fprintf (fptr, "%d", item);
/* append to tally.out */

Character at a Time I/O

*

There are three functions for doing single character I/O:
int getc (FILE* fp);
void putc (char c, FILE* fp);
void ungetc (char c, FILE* fp);

*

The getc()procedure reads a single character from the stream pointed to by the parameter.  The value returned is the character read, or the special value EOF if the end- of-file is encountered.
The
putc()procedure writes the character c, onto the stream specified by the parameter fp
ungetc()puts the last character read by getc() back on the input stream.  Very useful

*

*

1 February, 1998

3

CopyrightUniversity of Alberta