【发布时间】:2012-01-11 23:52:54
【问题描述】:
我正在尝试读取每行包含一个单词的文本文件的每一行,并将这些单词放入一个向量中。我该怎么做呢?
这是我的新代码:我觉得还是有问题。
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
std::string line;
vector<string> DataArray;
vector<string> QueryArray;
ifstream myfile("OHenry.txt");
ifstream qfile("queries.txt");
if(!myfile) //Always test the file open.
{
cout<<"Error opening output file"<<endl;
system("pause");
return -1;
}
while (std::getline(qfile, line))
{
QueryArray.push_back(line);
}
if(!qfile) //Always test the file open.
{
cout<<"Error opening output file"<<endl;
system("pause");
return -1;
}
while (std::getline(qfile, line))
{
QueryArray.push_back(line);
}
cout<<QueryArray[0]<<endl;
cout<<DataArray[0]<<endl;
}
【问题讨论】:
-
到目前为止的代码有什么问题?
-
@Mahesh 这个 if(!myfile) 可能是第一个问题。 (对不起..需要学习STL。)
-
@RomanB:那行没有错。
-
@DeadMG 现在我明白了为什么运算符重载会受到如此批评。