Professional Documents
Culture Documents
Reading (r)
/* Program to display the contents of a file on screen */ #include <stdio.h> void main() { FILE *fp; char c; fp = fopen("prog.c","r"); c = fgetc(fp) ; while (c!= EOF) { putchar(c); c = fgetc(fp); } fclose(fp); }
Closing a file
#include <stdio.h> int main( ) { FILE *fp; char c; fp = fopen("TENLINES.TXT", "r"); if (fp == NULL) printf("File doesn't exist\n"); else { c = fgetc(fp); /* get one character from the file*/ do { putchar(c); /* display it on the monitor*/ c = fgetc(fp); /* get one character from the file*/ } while (c != EOF); /* repeat until EOF (end of file)*/ } fclose(fp); return(0); }