In this tutorial you will learn about the C Program to Read & Display File and its application with practical example.
C Program to Read & Display File
In this tutorial, you will learn about the C Program to Read & Display File with a practical example.
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- Concepts of while loop.
- Conditional Statements in C programming.
- Using file functions of c language.
Program to reading & Display File
As we all know the file is a collection of characters, integers, and many data types. In strings, only one variable is declared which can store multiple values. First will take the file from the user. Then will Check that file if it’s empty or not. The C programming language has many pre-defined functions for file manipulation. but in today’s tutorial, Read & Display File from the user.
With the help of the below program, we can Read & Display.
1. Declaring the variables for the program.
2. Taking the input from the user.
3. Reading the file.
4. <strong>Displaying</strong> the file if there is any data in it.
5. Printing the results.
6. End program.
/* C Program to Read & Display File */
//declaring the vaiables for the program
char fname, str;
//Taking the input file name to read and print on screen using c program
printf("Enter the Name of File: ");
//getting the file name by the user.
fp = fopen(fname, "r");
//if the file is null then the program execution will end .
printf("Error Occurred while Opening the File!");
//redaing and printing the data form the file on the screen.
fscanf(fp, "%[^\0]", str);
printf("\nContent of File is:\n\n");
//Closing the file open by the program to read
In the above program, we have first initialized the required variable.
- *fp = it will hold the address value.
- str = it will hold the string data.
Taking input file name.
Opening the file and reading it.
Displaying the data from the file.
Closing the file.