0

I'm having trouble, and I'm not entirely sure if it's because I'm making a mistake I'm not aware of. I'm new to C++, I'm a Java programmer so I'm familiar with the code.

My question is, I'm reading strings from a text file, and putting it into a vector<string> container.

Then I'm reading each of the files stored in said container and I'm opening text files according to each string within the container (I've created sample text files according to each string in the vector container).

This is my code so far:

#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;
}

My output is :

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

I'm not sure why the last three input streams are not working.Thanks

0x499602D2
  • 87,005
  • 36
  • 149
  • 233
drocktapiff
  • 107
  • 2
  • 11

0 Answers0