Definition | #include <stdio.h> int getc(FILE *fp);
| |
Return val. | Character read as a positive | |
if successful. | ||
EOF | in case of an error or end of file. | |
Notes | getc is implemented both as a macro and as a function (see section “Functions and macros”). The call If you use a comparison such as If The following applies in the case of text files with SAM access mode and variable record | |
Example | The following program reads a file with file pointer fp one character at a time until end of file #include <stdio.h> #include <stdlib.h> int main(void) { int c, i = 0; char buf[BUFSIZ]; FILE *fp; char name[40]; printf("Please enter file to be read\n"); scanf("%s", name); if(( fp = fopen(name, "r")) == NULL) { perror("fopen"); /* Abort with error message 'fopen' if */ exit(1); /* file does not exist */ } while (( c = getc(fp)) != EOF ) buf[i++] = c; puts(buf); fclose(fp); return 0; } | |
See also | fgetc, getchar, getwc, ungetc, fopen, fopen64 |