I have to change code which opens the sum.txt and prints it to the screen. I now
ID: 3625141 • Letter: I
Question
I have to change code which opens the sum.txt and prints it to the screen.
I now need to print the contents from the sum.txt file one character at a time to the screen. Also, the code should work for any .txt file of any contents. See code and sample of sum.txt file.
CODE
#include
#include
#include
using namespace std;
int main()
{
string line;
ifstream file ("sum.txt");
if(file.is_open())
{
while(file.good())
{
getline(file,line);
cout << line << endl;
}
file.close();
}else
cout << "Unable to open file." << endl;
return 0;
}
SAMPLE sum.txt
2 and 2 are 4
4 and 4 are 8
Explanation / Answer
please rate - thanks
hope you don't mind I rewrote the code
#include <iostream>
#include <fstream>
using namespace std;
int main()
{char filename[30];
char c;
cout<<"what is the name of the file you are using? ";
cin>>filename;
ifstream input;
input.open(filename);
if(input.fail())
{ cout<<"file did not open please check it ";
system("pause");
return 1;
}
input.get(c);
while(input)
{cout<<c;
input.get(c);
}
input.close();
system("pause");
return 0;
}