Comma delimited fields to a map.

Apr 25, 2017 at 6:47pm
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
Apr 25, 2017 at 7:55pm
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);
    }
}

Apr 25, 2017 at 7:56pm
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/
Apr 25, 2017 at 11:38pm
@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 ! :)

Apr 26, 2017 at 1:03am
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";
   }
}
Apr 26, 2017 at 2:27pm
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 Apr 26, 2017 at 2:31pm
May 1, 2017 at 9:20am
@gunnerfunner:
What does while (inFile) mean ? Is it a shortform for infile.good() etc ?
May 1, 2017 at 10:30am
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)
May 1, 2017 at 4:09pm
@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 ?

May 1, 2017 at 4:28pm
is the operator bool under member functions of basic_ios referring to the true/false result of ifstream ?

Yes.


May 1, 2017 at 4:42pm
further details here if you're interested: http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool
Topic archived. No new replies allowed.