Wednesday 6 June 2018

Program to read text from a file

#include <stdio.h>
#include <stdlib.h> // For exit() function
void main()
{
    char c[1000];
    FILE *fptr;
clrscr();
    if ((fptr = fopen("program.txt", "r")) == NULL)
    {
    printf("Error! opening file");
    // Program exits if file pointer returns NULL.
    exit(1);
    }

    // reads text until newline
    fscanf(fptr,"%[^\n]", c);

    printf("Data from the file:\n%s", c);
    fclose(fptr);
    getch();
}

OUTPUT:-

No comments:

Post a Comment