Reputation: 699
I'm trying to solve Problem 13 of project euler, which involves the sum of 100 50 digit numbers. I figured there would be a better way than the paste that whole chunk of numbers into my code. So I searched around and found that you could paste the chunk into a .txt file and read it from there.
So, how would I go about reading from a .txt file in C++ and more importantly getting the 50 digit strings individually from it?
Upvotes: 0
Views: 232
Reputation: 2143
Something like this?
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("numbers.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
int i = atoi(line.c_str());
// do here something with 'i'
cout << i
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Upvotes: 2