Reputation: 79
I have data in a text file which I wish to read in and split up to then create a new object out of.
I have found this code:
std::ifstream file("plop");
std::string line;
while(std::getline(file, line))
{
std::stringstream linestream(line);
std::string data;
int val1;
int val2;
std::getline(linestream, data, '\t');
linestream >> val1 >> val2;
}
Which reads in a text document and splits it by line. However this code assumes the delimiter is always a tab. What if the data had more than one delimiter which would point to what type of data would follow it. i.e assuming a text file such as:
hey, "hi" (hello) [hola]
bye, "by" (byeee) [biii]
and I wanted to split the data into
String twoCharacters;
String threeCharacters;
String fourCharacters;
String fiveCharacters;
so
twoCharacters = hi and by
with the delimiter being two " and
threeCharacters = hey and bye
with the delimiter being a , after it
Any help would be greatly appreciated! Thanks.
Upvotes: 1
Views: 1906
Reputation: 48615
You can just keep calling std::getline()
with different delimiters:
std::ifstream file("test.txt");
std::string line;
while(std::getline(file, line))
{
std::stringstream linestream(line);
std::string skip;
std::string item1;
std::string item2;
std::string item3;
std::string item4;
std::getline(linestream, item1, ',');
std::getline(linestream, skip, '"');
std::getline(linestream, item2, '"');
std::getline(linestream, skip, '(');
std::getline(linestream, item3, ')');
std::getline(linestream, skip, '[');
std::getline(linestream, item4, ']');
if(linestream) // true if there were no errors reading the stream
{
std::cout << item1 << '\n';
std::cout << item2 << '\n';
std::cout << item3 << '\n';
std::cout << item4 << '\n';
}
}
I used variable skip
to read up to the beginning of the next field.
Upvotes: 1
Reputation: 1394
You could use a std::istream::sentry
to do this. While a little complicated, you're given almost complete control over how your data is input.
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
std::istream& operator>>(std::istream& is,
std::map<std::string, std::vector<std::string> >& map) {
std::istream::sentry s(is);
if(s) {
// your accumulator
std::string str1;
// while your stream is good keep appending data
while(is.good()) {
// get the next character in the stream
char c = is.get();
// if it is a comma append the data to the "three" vector
if(c == ',') {
map["three"].push_back(str1);
// if it is a comma get all of the data until the next comma
} else if(c == '"') {
std::string str2;
str2 += c;
c = is.get();
while(c != '"') {
str2 += c;
c = is.get();
}
str2 += c;
map["two"].push_back(str2);
// do the same thing for parenthases
} else if(c == '(') {
std::string str2;
str2 += c;
c = is.get();
while(c != ')') {
str2 += c;
c = is.get();
}
str2 += c;
map["five"].push_back(str2);
// do the same thing for square brackets
} else if(c == '[') {
std::string str2;
str2 += c;
c = is.get();
while(c != ']') {
str2 += c;
c = is.get();
}
str2 += c;
map["four"].push_back(str2);
// append to your accumulator if you get an alphanumeric char
} else if(std::isalnum(c)) {
str1 += c;
// clear your accumulator if you get a return
} else if(c == '\n') {
str1.clear();
}
}
}
return(is);
}
int main() {
std::ifstream file("plop");
// make your map to hold your data
std::map<std::string, std::vector<std::string> > map;
map["two"] = std::vector<std::string>();
map["three"] = std::vector<std::string>();
map["four"] = std::vector<std::string>();
map["five"] = std::vector<std::string>();
// read your data
file >> map;
// print your data
std::vector<std::string> words{"two", "three", "four", "five"};
for(auto x: words) {
std::cout << x << std::endl;
for(auto y: map[x]){
std::cout << '\t' << y << std::endl;
}
}
}
This should print
two
"hi"
"by"
three
hey
bye
four
[hola]
[biii]
five
(hello)
(byeee)
Upvotes: 0