我正在尝试解决euler项目的问题13,它涉及100个50位数字的总和。我认为有一种比将整块数字粘贴到我的代码中更好的方法。所以我到处搜索,发现您可以将块粘贴到.txt文件中并从那里读取它。
那么,如何从C++中的.txt文件中读取,更重要的是,如何从该文件中分别获取50位字符串呢?
发布于 2012-04-04 18:01:29
像这样的东西?
// 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;
}https://stackoverflow.com/questions/10008844
复制相似问题