如何拆分文件行与空格和制表区别?

Joh*_*ohn 8 c++ string whitespace tabs split

我有一个以下格式的文件

mon 01/01/1000(TAB)你好(TAB)你好吗?

有没有办法以这种方式阅读文本'\t'单独作为分隔符(而不是空格)?

所以样本输出可以是,

星期一01/01/1000

你好

你好吗

我无法使用fscanf(),因为它只读到第一个空格.

jro*_*rok 11

仅使用标准库设施:

#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);
}
Run Code Online (Sandbox Code Playgroud)

您可以在这里获得更多想法:如何在C++中对字符串进行标记?


Wea*_*Fox 5

从提升:

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

您可以在其中指定任何定界符。