Q:

C program to illustrate reading of data from a File

belongs to collection: C programs - File Handling

0

This C Program illustrates reading of data from a file. The program opens a file which is present. Once the file opens successfully, it uses fgetc() library call to read the content.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C program to illustrate reading of data from a File - Source code

void main()
{
    FILE *fptr;
    char filename[15];
    char ch;
 
    printf("Enter the filename to be opened \n");
    scanf("%s", filename);
    /*  open the file for reading */
    fptr = fopen(filename, "r");
    if (fptr == NULL)
    {
        printf("Cannot open file \n");
        exit(0);
    }
    ch = fgetc(fptr);
    while (ch != EOF)
    {
        printf ("%c", ch);
        ch = fgetc(fptr);
    }
    fclose(fptr);
}

Program Output

Enter the filename to be opened
emp.rec
Name    = Suresh
Age     = 25
Salary  = 50000.00

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C Program to Find the Number of Lines in a Text Fi... >>
<< C program to create a File and store information...