Single
Character I/O
There are commonly four
functions/macros which perform single character input/output to or from files.
They are analogous to the functions/macros
getchar()
putchar()
for the standard I/O files and they
are called:
getc()
ungetc();
putc()
fgetc()
fputc()
getc()
and fgetc()
The difference between getc()
and fgetc() will depend upon a particular system. It might be that
getc() is implemented as a macro, whereas fgetc() is
implemented as a function or vice versa. One of these alternatives may not be
present at all in a library. Check the manual, to be sure! Both getc()
and fgetc() fetch a single character from a file:
FILE
*fp;
char
ch;
/*
open file */
ch
= getc (fp);
ch
= fgetc (fp);
These functions return a character
from the specified file if they operated successfully, otherwise they return EOF
to indicate the end of a file or some other error. Apart from this, these
functions/macros are quite unremarkable.
ungetc()
ungetc() is a function which `un-gets' a character from a file. That
is, it reverses the effect of the last get operation. This is not like writing
to a file, but it is like stepping back one position within the file. The
purpose of this function is to leave the input in the correct place for other
functions in a program when other functions go too far in a file. An example of
this would be a program which looks for a word in a text file and processes
that word in some way.
while
(getc(fp) != ' ')
{
}
The program would skip over spaces
until it found a character and then it would know that this was the start of a
word. However, having used getc() to read the first character of that word, the position in
the file would be the second character in the word! This means that, if another
function wanted to read that word from the beginning, the position in the file
would not be correct, because the first character would already have been read.
The solution is to use ungetc() to move the file position back a character:
int
returncode;
returncode
= ungetc(fp);
The returncode is EOF if the
operation was unsuccessful.
putc()
and fputc()
These two functions write a single
character to the output file, pointed to by fp. As with getc(),
one of these may be a macro. The form of these statements is:
FILE
*fp;
char
ch;
int
returncode;
returncode
= fputc (ch,fp);
returncode
= putc (ch,fp);
The returncode is the ascii code of
the character sent, if the operation was successful, otherwise it is EOF.
fgets()
and fputs()
Just as gets()
and puts() fetched and sent strings to standard input/output files
stdin and stdout, so fgets() and fputs() send strings to generalized files. The form of an fgets() statement is as follows:
char
*strbuff,*returnval;
int
n;
FILE
*fp;
returnval
= fgets (strbuff,n,fp);
strbuff is a pointer to an input buffer for the string; fp is a
pointer to an open file. returnval is a pointer to a string: if there was an
error in fgets() this pointer is set to the value NULL,
otherwise it is set to the value of "strbuff". No more than (n-1)
characters are read by fgets() so the programmer has to be sure to set n equal to the size
of the string buffer. (One byte is reserved for the NULL terminator.) The form
of an fputs() statement is as follows:
char
*str;
int
returnval;
FILE
*fp;
returnval
= fputs (str,fp);
Where str is the NULL
terminated string which is to be sent to the file pointed to by fp.
returnval is set to EOF if there was an error in writing to the file.
feof()
This function returns a true or
false result. It tests whether or not the end of a file has been reached and if
it has it returns `true' (which has any value except zero); otherwise the
function returns `false' (which has the value zero). The form of a statement
using this function is:
FILE
*fp;
int
outcome;
outcome
= feof(fp);
Most often feof()
will be used inside loops or conditional statements. For example: consider a
loop which reads characters from an open file, pointed to by fp.
A call to feof() is required in order to check for the end of the file.
while
(!feof(fp))
{
ch = getc(fp);
}
Translated into pidgin English, this
code reads: `while NOT end of file, ch equals get character from file'. In
better(?) English the loop continues to fetch characters as long as the end of
the file has not been reached. Notice the logical NOT operator !
which stands before feof().
0 comments:
Post a Comment