C Program to illustrate how to store & read data on a disk

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

In this c program, we have to discuss how the data stored on the disc is read. Here the program opens a file that is present. This program reads a file whose name is entered by a user and displays its contents on the screen. Opening a file means bringing the file contents from disc to ram for performing operations in it.

How to open a file in C?

Here the function fopen is used to open a file. Fopen will return a pointer if the file is successfully opened; otherwise, it will return null. Fclose function is used to close a file. In this c program, library function fgetc is used to return a character that is read from the file.

The program's main logic is to declare the variable filename, ch as a character type, and *fptr of type file. Then read the name of the file which the user wants to open into the variable filename. Then open the file using the function fopen.

Then check if fptr is null or not. If fptr is null, then display we cannot open the file. Else read the first character from the file into the variable ch using the function fgetc(fptr). Then by using a while loop check 'ch' not equal to the end of the file, display ch, and set ch=fgetch(fptr).

After displaying every character, close the file using the function fclose(). This is a simple c program that uses only simple library functions.

Which are the critical operations performed on a file in C?

Different Operations can be performed on a File. Important among them are listed below.

  1. Making of a new File
  2. Opening an existing File using fopen
  3. Reading data from the File using fscanf or fgetc
  4. Writing data to a File using fputs or fprintf
  5. Moving the File to a specific location using fseek or rewind
  6. Closing of the File using fclose

ALGORITHM

STEP 1: Include the header files to use the built-in functions in the C program.

STEP 2: Declare the variable *ftpr as of type FILE, declare the filename, ch as a character type.

STEP 3: Read the filename, which is to be opened from the user into the variable filename using the function gets().

STEP 4: Open the file using the function fopen with read mode.

STEP 5: If fpr=null, then the display cannot open the file else do step 6.

STEP 6: Assign ch=fgetc(fptr) means read the first character from the file.

STEP 7: By using a while loop, check ch!=EOF then display ch.

STEP 8: Assign ch=fgetc(fptr).And repeat step 7.

STEP 9: Close the file using the function fclose().

C Source Code

                                          #include <stdio.h>
#include <stdlib.h>

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

OUTPUT

Enter the file name to be opened
emp.rec

Name    = Ajay
Age     = 25
Salary  = 25000.00