1

I am trying to open a text file, and from this, read each line, and map each word occurrence to the line number it is on. Then, I want to print the map. Here is my code:

#include <map>
#include <set>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main(){

std::map<string, set<int>> myMap;
ifstream myFile;
string myLine ="";
int lineNum = 0;
stringstream myStream;
string myWord ="";
set<int> mySet;

myFile.open("myTextFile.txt");
if(myFile.is_open()){
    while (!myFile.eof()){
        getline(myFile, myLine);
        myStream.str(myLine);
        ++lineNum;
        while (!mySStream.eof())
        {
            myStream >> myWord;
            myMap[myWord].insert(lineNum);

        }           
        myStream.clear();
   }
}
myFile.close();
// at this point, I expect everything to have been mapped
// mapping each word occurrence to a line
//I now want to print the map but out does not work, and I need to use an iterator
//I have tried this:
map<string, set<int>>::iterator iter;
for (iter = myMap.begin(); iter != myMap.end(); ++iter)
{
    cout << "Key: " << iter->first << endl << "Values:" << endl;
    set<int>::iterator setIter;
    for ( setIter = iter->second.begin(); setIter != iter->second.end(); ++setIter)
        cout << " " << *setIter<< endl;
}
return 0;

}

I have no output. Why not? Secondly, is everything being properly mapped?

JCoder
  • 189
  • 1
  • 3
  • 17

1 Answers1

1

I haven't been able to test it as I am not at my development machine. This should work if I understand what you are trying to do. Basically, instead of checking the EOF bit, I moved the getlines into the while loops. std::getline will exit the loop after it has read the entire file!

#include <map>
#include <set>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;
int main(){
std::map<string, set<int>> myMap;
ifstream myFile;
string myLine ="";
int lineNum = 0;
stringstream myStream;
string myWord ="";
set<int> mySet;

myFile.open("myTextFile.txt");

if(myFile.is_open()){
    while (getline(myFile, myLine)){
        myStream.str(myLine);
        ++lineNum;
        while (myStream >> myWord)
        {
            myMap[myWord].insert(lineNum);    
        }           
        myStream.clear();
   }
    myFile.close();
}
else
{
    std::cout << "Unable to open file!" << std::endl;
}

map<string, set<int>>::iterator iter;
for (iter = myMap.begin(); iter != myMap.end(); ++iter)
{
    cout << "Key: " << iter->first << endl << "Values:" << endl;
    set<int>::iterator setIter;
    for (setIter = iter->second.begin(); setIter != iter->second.end(); ++setIter)
    {
        cout << " " << *setIter<< endl;
    }
}
return 0;

}
yash101
  • 661
  • 1
  • 8
  • 20
  • I can test the program if you can give a copy of the input file. – yash101 Nov 27 '14 at 04:07
  • I'm not getting any output at all. Also, why would I not be able to use while (!myFile.eof())? I was taught this way. – JCoder Nov 27 '14 at 05:12
  • I got no output either. Perhaps your file is not open? According to [link](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong?lq=1), eof() is checked afterwards. On the other hands, this code is a bit more concise. std::getline() and operator>>() both check for fail(). – yash101 Nov 27 '14 at 05:29
  • I will add a small piece of code to issue an error if it can't open the file! – yash101 Nov 27 '14 at 05:29
  • it's getting the error message. I've tried on both Mac and PC. On Mac I'm forced to use rtf. – JCoder Nov 27 '14 at 05:45
  • You cannot use RTF. You must use plain text. RTF itself is a file format. It is quite structured in general, but will look like gibberish to your software – yash101 Nov 27 '14 at 06:11
  • I thought that might be it. But does that mean it cannot be done on Mac? I don't think Mac can support txt. But, this is not working on my PC either. – JCoder Nov 27 '14 at 06:12
  • @JCoder : "*Also, why would I not be able to use while (!myFile.eof())? I was taught this way.*" By who? It's always wrong. "*I don't think Mac can support txt.*" It most definitely can. – ildjarn Nov 27 '14 at 06:49
  • When I was going to school. And I tried, but it wanted to rename it file.txt.rtf – JCoder Nov 27 '14 at 07:01
  • @JCoder : Your school taught incorrectly. ;-] And you can create a new "text" file in one line of C++ code – hint: any empty file is an empty "text" file, regardless of filename. – ildjarn Nov 27 '14 at 08:07
  • I am using a macintosh for my development. In the worst case, write the code in RTF and copy/paste it into this terminal editor called nano. Just open terminal and type in `nano [filename]`. Press [ctrl]+x, y and [return] to save and exit – yash101 Nov 27 '14 at 15:35
  • By error, I mean, when the program is running, it will say Unable to open file! It won't give you a compiler error – yash101 Nov 27 '14 at 15:37