Develop a C program to read a file and display on screen
Code
make sample.txt file in same directory and write some text inside it
#include<stdio.h>#include <stdlib.h> // for exit() functionint main(){char c[1000];FILE *fptr;if ((fptr = fopen("sample.txt", "r")) == NULL){printf("Error! opening file\n");// exit from program if file pointer returns NULL.exit(1);}// read the text until newlinefscanf(fptr,"%[^\n]", c);printf("Data from the file:\n%s", c);fclose(fptr);return 0;}
Output
0 Comments
Post a Comment