Simple Program for Read File Operation
On this page (6sections)
Program for reading the content of a file.
Simple Program for Read File Operation Algorithm/Steps:
- STEP 1: Start the program.
- STEP 2: Declare the variables.
- STEP 3: Get the file name to read.
- STEP 4: Using ifstreamin(filename) check whether the file exist.
- STEP 5: If the file exists then check for the end of file condition.
- STEP 6: Read the contents of the file.
- STEP 7: Print the contents of the file.
- STEP 8: Stop the program.
Simple Program for Reading File Operation Example
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main() {
char c, fname[10];
clrscr();
cout << "Enter file name:";
cin>>fname;
ifstream in(fname);
if (!in) {
cout << "File Does not Exist";
getch();
return;
}
cout << "\n\n";
while (in.eof() == 0) {
in.get(c);
cout << c;
}
getch();
}
Sample Output
Enter File name: one.txt
Master of Computer Applications
(file one.txt contains text "Master of Computer Applications")
How It Works
This C++ program demonstrates Simple Program for Read File Operation. It first reads the required values as input, then walks through the data with a loop to compute the result, and finally prints the output shown in the Sample Output above.
- Declare the variables that hold the program’s data.
- Read the input values that the program will work with.
- Iterate over the data using a loop to apply the logic.
- Use conditional statements to handle the different cases.
- Print the final result to the console so you can compare it with the sample output.
Try changing the input values and re-running the program to see how the output changes — this is the fastest way to understand how the logic behaves.
Related Pages
Learn the concept first, then study the code:
- C++ Programs — Browse all C++ Programs.
Frequently Asked Questions
What does this C++ program do?
It is a C++ example program that demonstrates Simple Program for Read File Operation, including the complete source code and the expected sample output.
How do I compile and run this C++ program?
Save the code in a `.cpp` file, compile it with `g++ filename.cpp -o program`, then run it with `./program` (or `program.exe` on Windows).
What concepts does this example use?
This example uses loops to iterate over data, arrays and conditional logic, illustrating a common pattern in C++ programming.