我有一个以下格式的文件
星期一 01/01/1000(TAB)嗨 你好(TAB)你好吗
有没有什么方法可以用这样的方式来读取文本'\t'
单独作为分隔符(而不是空格)?
所以样本输出可以是,
星期一 01/01/1000
你好
你好吗
我无法使用fscanf()
,因为它只读取到第一个空格。
仅使用标准图书馆设施:
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
std::ifstream file("file.txt");
std::string line;
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);
}
您可以在这里获得更多想法:如何在 C++ 中标记字符串?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)