Write a C++ program to read the content of a file.




Program Algorithm:
Step 1: Include the header files
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 exist 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.


Program Code in C++:
#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();
}





Program Output:
Enter File name: hello.txt
Master of Computer Applications
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: