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
#include <stdio.h> /* for fgetc, printf, */
/* fclose, FILE, */
/* NULL, EOF */
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|
|