How to split file with space and tab difference

I have a file in the format below

mon 01/01/1000 (TAB) hello hello (TAB) how are you

Is there a way to read the text in such a way as to use \ t only as a delimiter (and not a space)?

So the output can be

mon 01/01/1000

Hi Hi

how are you

(cannot use fscanf since it reads to the first space)

+4
source share
2 answers

Using only standard library tools:

#include <sstream> #include <fstream> #include <string> #include <vector> std::ifstream file("file.txt"); std::string line; std::string partial; std::vector<std::string> tokens; while(std::getline(file, line)) { // '\n' is the default delimiter std::istringstream iss(line); std::string token; while(std::getline(iss, token, '\t')) // but we can specify a different one tokens.push_back(token); } 

Here you can get some more ideas: How do I want tokenize a string in C ++?

+6
source

from boost:

 #include <boost/algorithm/string.hpp> std::vector<std::string> strs; boost::split(strs, "string to split", boost::is_any_of("\t")); 

you can specify any separator there.

+3
source

Source: https://habr.com/ru/post/1412761/


All Articles