1 2 3 4 5 6

*

The special functions getchar() and putchar() are perhaps best forgotten from now on.

Memory I/O

*

One can also do input output to an array or other region in memory.  The relevant prototypes are:
int sscanf (char* buffer, char* format, *arg1, *arg2, ..); char* sprintf (char* buffer, char* format, arg1, arg2, ..);

*

*

sscanf() and sprintf() allow you to read and write to a memory buffer, just as if it were a file.
That is, to use a pointer to an array or memory, just as if it were a pointer to a file.

Line at a time I/O

*

There are two functions for moving a whole line of data from/to a file:
char* fgets (char* buffer, int BLength, FILE* fp);
void fputs (char* buffer, FILE* fp);

*

These functions require care in their usage. fgets()copies all the characters until a '\n' into the array pointed to by buffer, replacing the '\n' by '\0'.  To protect against overflow, only BLength-1 characters are moved. Fgets() return NULL when an EOF is seen. fputs()copies the contents of a null-terminated string to the output file, replacing the terminating '\0' with '\n' as one would hope.

1 February, 1998

4

CopyrightUniversity of Alberta