1

I cannot seem to get my file "paths.txt" to read into my map file. I cannot figure out what I am doing wrong. I'd appreciate any pointers. The end result I want is to take each of the key value pairs into the map.

The format of my file is

192, 16 120, 134 256, 87 122, 167 142, 97 157, 130 245, 232 223, 63 107, 217 36, 63 206, 179 246, 8 91, 178

And my code is

ifstream myfile("paths.txt"); 
    std::map<int, int> mymap;
    int a, b;
    while (myfile.good())
    {
    myfile >> a >> b;
    mymap[a] = b;
    }
    mymap;
Phi
  • 231
  • 7
  • 21
  • Get rid of the commas, non-whitespace separators are causing the stream to fail. Also [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user657267 Sep 30 '15 at 04:40
  • @user657267 Anyway to code it without deleted 65000 ,'s? The file has a lot of commas :-D – Phi Sep 30 '15 at 04:43

1 Answers1

2
  1. You don't have anything to read the commas in your text file.
  2. Also , instead of while (myfile.good()), use while ( myfile >> ...).
std::map<int, int> mymap;

char dummy;
int a, b;
while (myfile >> a >> dummy >> b)
{
   mymap[a] = b;
}
R Sahu
  • 196,807
  • 13
  • 136
  • 247
  • @R Sahu that worked awesome super thanks. I thought for some reason that it skipped ,'s when reading ints. – Phi Sep 30 '15 at 04:47