0

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?

4

1 回答 1

2

像这样的东西?

// 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;
}
于 2012-04-04T10:01:29.057 回答