Mega Code Archive

 
Categories / C / Code Snippets
 

Reads a character from stream

//Header file: #include <stdio.h> //Declaration: int getc(FILE *stream); //Return: returns EOF on file end or error. #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { FILE *filep; char ch; if((filep=fopen("testfile", "r"))==NULL) { printf("Cannot open file.\n"); exit(1); } while((ch=getc(filep))!=EOF) { printf("%c", ch); } fclose(filep); return 0; }