输入流c ++

时间:2014-02-03 20:05:29

标签: c++

我遇到了麻烦,而且我不确定是不是因为我犯了一个我不知道的错误。我是C ++的新手,我是一名Java程序员,所以我熟悉这些代码。

我的问题是,我正在从文本文件中读取字符串,并将其放入vector<string>容器中。

然后我读取存储在所述容器中的每个文件,并根据容器中的每个字符串打开文本文件(我根据每个字符串创建了示例文本文件。矢量容器)。

到目前为止,这是我的代码:

#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <fstream>
using namespace std;

vector<string> getIndexVector(){
    ifstream fin("documentIndex.txt");
    if (!fin.good())
        cout << "Document Index Text File not Found" << endl;
    vector<string> di;
    string textLine;
    while (!fin.eof()){
        fin >> textLine;
        cout << textLine << endl;
        di.push_back(textLine);     
    }
    return di;
}
map<string, string> getDocumentMap(vector<string> &vs){
    map<string, string> dm;
    string documentContent;
    string documentName;
    vector<string>::iterator it;
    for (it = vs.begin(); it != vs.end(); ++it){
        documentName = *it;
        ifstream fin2(documentName);
        if (!fin2.good()){
            cout << "Document could not open" << endl;
        }
        fin2 >> documentContent;
        cout << documentContent << endl; 
        dm.insert(pair<string, string>(documentName, documentContent));
    }

    return dm;
}


int main(){
    vector<string> docIndex = getIndexVector();
    map<string, string> documentMap = getDocumentMap(docIndex);
    cin.get();
    return 0;
}

我的输出是:

document1
document2
document3
Document could not open
Document could not open
Document could not open

我不确定为什么最后三个输入流不起作用。谢谢

0 个答案:

没有答案