Oct 1, 2017 06:49 PM
(This post was last modified: Oct 1, 2017 07:01 PM by AliveAgain.)
Using this three column tab delimited text file: https://pastebin.com/vdgcyzfm
I want to read each line > delimit at tab> append each word from each column into a separate vector string
ie. col1, col2, col3
col1 = artless, bawdy, ... etc..
..
..
I know how to read each line, but I'm having trouble finding a simple way to split the lines into words and appending them into their respective column vector.
DO NOT DEAL WITH THIS MEMBER
Oct 1, 2017 06:50 PM
(This post was last modified: Oct 1, 2017 06:54 PM by Mouse❤️.)
strtok with \t as the delimiter
http://www.cplusplus.com/reference/cstring/strtok/
PHP Code
<?php #include <stdio.h> #include <string.h>
int main() { char data[] = "Hello,\tmy\tname\tis\tOliver"; printf("%s\n\n", data); char* piece = strtok(data,"\t"); do { printf("%s\n",piece); } while ((piece = strtok(NULL,"\t")));
return 0; }
(Oct 1, 2017 06:50 PM)Mouse❤️ Wrote: strtok with \t as the delimiter
http://www.cplusplus.com/reference/cstring/strtok/
Thanks I'll try it out.
DO NOT DEAL WITH THIS MEMBER
Posted an example for reference.
(Oct 1, 2017 06:54 PM)Mouse❤️ Wrote: Posted an example for reference.
I should have specified I'm using c++, so I'd have to convert my string to a cstring. I'll try it anyways I guess.
DO NOT DEAL WITH THIS MEMBER
Oct 1, 2017 07:39 PM
(This post was last modified: Oct 1, 2017 07:39 PM by Mouse❤️.)
In that case, you're going to want to use a stringstream with a back_insert_iterator to do the job. Example here:
PHP Code
<?php #include <iostream> #include <sstream> #include <vector> #include <iterator>
int main() { std::string s = "Hello\tmy\tname\tis\tOliver"; std::stringstream ss; ss.str(s); std::string item; std::vector<std::string> pieces; while (std::getline(ss, item, '\t')) { *(std::back_insert_iterator<std::vector<std::string>>(pieces)++) = item; } for (std::string p : pieces) std::cout << p << std::endl; }
Oct 1, 2017 08:01 PM
(This post was last modified: Oct 1, 2017 08:02 PM by AliveAgain.)
This is my code currently: Code void InsultGenerator::initialize(){
string line;
vector<string> insults;
ifstream file("InsultsSource.txt");
if(file.is_open()){
while(getline(file, line)){
stringstream ss;
ss.str(line);
string word;
while(getline(ss, word, '/t')){
cout << word << endl;
insults.push_back(word);
}
}
file.close();
}else{
cout << "unable to open file\n";
}
}
But I get error: no matching function for call to 'getline'. Parameter type mismatch: types char and 'stringstream' are not compatible. etc.. (using namespace std)
A good reason why not to use 'using namespace std;' as pointed out in pretty much any tutorial you'll find. GCC has extensions to this function btw.
Oct 1, 2017 08:40 PM
(This post was last modified: Oct 1, 2017 08:51 PM by AliveAgain.)
(Oct 1, 2017 08:25 PM)0xDEAD10CC Wrote: A good reason why not to use 'using namespace std;' as pointed out in pretty much any tutorial you'll find. GCC has extensions to this function btw.
I get the errors with or without using namespace std.
error: no matching function for call to 'getline'
while(std::getline(ss, word, '/t')){
(Oct 1, 2017 08:40 PM)AliveAgain Wrote: I get the errors with or without using namespace std.
error: no matching function for call to 'getline'
while(std::getline(ss, word, '/t')){
Ah, '/t' was supposed to be '\t'. Such a stupid mistake LOL
Sucks the error's aren't more specific.
|