fastread homefastrread library fastread menu

C : Read a String of Text from File

Tutorial by:Manisha Dubey      Date: 2016-06-17 04:44:50

❰ Previous Next ❱

This program reads a string of text from a file.


/* Source Code to read a text of string from a file. */

#include <stdio.h>
#include <stdlib.h> /* For exit() function*/
int main()
{
   char c[1000];
   FILE *fptr;
   if ((fptr=fopen("program.txt","r"))==NULL){
       printf("Error! opening file");
       exit(1);         /* Program exits if file pointer returns NULL. */
   }
   fscanf(fptr,"%[^\n]",c);
   printf("Data from file:\n%s",c);
   fclose(fptr);
   return 0;
}

This program reads the content of program.txt file and stores it in a string c if of this file and this c program is same and the name of file matches but, if this there is no file named program.txt then, this program displays Error! opening file and program will be terminated.

❰ Previous Next ❱


C

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website