I would like to read and display the content of a file entered by user at run-time
My code :
#include<iostream>
#include<fstream>
#include<stdio.h>
using namespace std;
int main()
{
char fileName[30], ch;
fstream fp;
cout<<"Enter the Name of File: ";
gets(fileName);
fp.open(fileName, fstream::in);
if(!fp)
{
cout<<"\nError Occurred!";
return 0;
}
cout<<"\nContent of "<<fileName<<":-\n";
while(fp>>noskipws>>ch)
cout<<ch;
fp.close();
cout<<endl;
return 0;
}
output :
C:\Users\prade\Desktop>g++ -o file file.cpp&file.exe
Enter the Name of File: tt.txt
Content of tt.txt:-
Hello , I am c++.
I am from us.
I am a programmer.
C:\Users\prade\Desktop>
I want to set the content of file to value of string str
. I want to print the whole file's content by using cout<<str;
How can I do that ?