Comma delimited fields to a map.

I have a plain text file in the following format:
1
2
aaaa,bbbb
cccc,dddd


Is there any way to input aaaa as the key and bbbb as the value into a std::map ?

I tried reading the file via getline(), but I couldn't figure out the logic required to input to a map
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <map>
 #include <string>
 #include <iostream>
 #include <fstream>
 using namespace std;
 
 int main ()
 {  ifstream    ifs ("data.txt");
    string      f1, f2;
    char        comma;
    map<string,string>  my_map;
    
    while (ifs >> f1 >> comma >> f2)
    {   pair<string,string> pr (f1,f2);
        my_map.insert (pr);
    }
}

Using getline is the right approach. However you need to split the string into key and value before you can insert them into a map. The string class has functions find and substr.

http://www.cplusplus.com/reference/string/basic_string/find/
http://www.cplusplus.com/reference/string/basic_string/substr/
@AbstractionAnon:

I tried a similar method using the >> operator, but robustness is an issue. Sometimes the output isn't correct for reasons unknown. Some values for the key have unicode characters, not sure if this is the problem. I was half thinking of using regex along with this method, but it got way over my head for now haha.

@Thomas1965:

find and substr looks interesting. Will take a look at it, thanks for the tip ! :)

You could read the file using the comma delimiter directly into the std::map:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <map>
#include <fstream>
#include <string>

int main()
{
   std::map<std::string, std::string> myMap{};
   std::ifstream inFile {"D:\\test.txt"};
   while (inFile)
   {
       std::string key{}, value{};
       {
            getline(inFile, key, ',') && getline(inFile, value);
            if(inFile)
            {
                    myMap[key] = value;
            }
        }
   }
   for (const auto& elem : myMap)
   {
       std::cout << elem.first << " " << elem.second << "\n";
   }
}
Some values for the key have unicode characters

Yes, that's going to be a problem with std::string. Use std::wstring instead.
http://www.cplusplus.com/reference/string/wstring/

Don't forget to use std::wifstream and std::wcout also.
http://www.cplusplus.com/reference/fstream/wifstream/
http://www.cplusplus.com/reference/iostream/wcout/


Last edited on
@gunnerfunner:
What does while (inFile) mean ? Is it a shortform for infile.good() etc ?
while loop - http://en.cppreference.com/w/cpp/language/while -
 
while ( condition ) statement 

Executes a statement repeatedly, until the value of condition becomes false

so in this case, condition is inFile and it is true if none of the stream's flags (badbit, eofbit, failbit) is set:
http://en.cppreference.com/w/cpp/io/basic_ifstream (see here for the flags and what they mean)
@gunnerfunner
Thanks for the reply, from the reference, is the operator bool under member functions of basic_ios referring to the true/false result of ifstream ?

is the operator bool under member functions of basic_ios referring to the true/false result of ifstream ?

Yes.


further details here if you're interested: http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool
Topic archived. No new replies allowed.