Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Easiest way to tokenize tab delimited lines read from file? C++

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.
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.
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.
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;
} 
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.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(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.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Mr. RobotSep 11, 2022 12:33 AM 1 156 Sep 11, 2022 12:33 AM
Last Post: 570RM
[Python] by Cabgun JockJun 10, 2019 08:13 AM 3 154 Jun 10, 2019 08:13 AM
Last Post: HFDone
[C#] by PawnhubMay 15, 2019 11:06 PM 0 155 May 15, 2019 11:06 PM
Last Post: Pawnhub