C Program to Read Data from File

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 libc fgetc() library call to read the content.

Here is source code of the C program to illustrate reading of data from a file. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program to illustrate how a file stored on the disk is read
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. void main()
  8. {
  9.     FILE *fptr;
  10.     char filename[15];
  11.     char ch;
  12.  
  13.     printf("Enter the filename to be opened \n");
  14.     scanf("%s", filename);
  15.     /*  open the file for reading */
  16.     fptr = fopen(filename, "r");
  17.     if (fptr == NULL)
  18.     {
  19.         printf("Cannot open file \n");
  20.         exit(0);
  21.     }
  22.     ch = fgetc(fptr);
  23.     while (ch != EOF)
  24.     {
  25.         printf ("%c", ch);
  26.         ch = fgetc(fptr);
  27.     }
  28.     fclose(fptr);
  29. }

$ cc pgm96.c
$ a.out
Enter the filename to be opened
pgm95.c
/*
 * C program to create a file called emp.rec and store information
 * about a person, in terms of his name, age and salary.
 */
 
#include <stdio.h>
 
void main()
{
    FILE *fptr;
    char name[20];
    int age;
    float salary;
 
    fptr = fopen ("emp.rec", "w"); /* open for writing*/
 
    if (fptr == NULL)
    {
        printf("File does not exists \n");
        return;
    }
    printf("Enter the name \n");
    scanf("%s", name);
    fprintf(fptr, "Name    = %s\n", name);
    printf("Enter the age \n");
    scanf("%d", &age);
    fprintf(fptr, "Age     = %d\n", age);
    printf("Enter the salary \n");
    scanf("%f", &salary);
    fprintf(fptr, "Salary  = %.2f\n", salary);
    fclose(fptr);
}

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on File Handling, go to C Programming Examples on File Handling. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.