6.18.15 fgetc Function
Get a character from a stream
Include
<stdio.h>
Prototype
int fgetc(FILE * stream);
Argument
stream
- pointer to the open stream
Return Value
Returns the character read or EOF
if a read error occurs or end-of-file
is reached.
Remarks
The function reads the next character from the input stream, advances the file-position
indicator and returns the character as an unsigned char
converted to an
int
.
Example
See the notes at the beginning of this chapter or section for
information on using printf()
or scanf()
(and other functions reading and writing the stdin
or
stdout
streams) in the example code.
#include <stdio.h>
int main(void)
{
FILE *buf;
char y;
if ((buf = fopen("afile.txt", "r")) == NULL)
printf("Cannot open afile.txt\n");
else
{
y = fgetc(buf);
while (y != EOF)
{
printf("%c|", y);
y = fgetc(buf);
}
fclose(buf);
}
}
Example Input
Contents of afile.txt
(used as input):
Short
Longer string
Example Output
S|h|o|r|t|
|L|o|n|g|e|r| |s|t|r|i|n|g|
|