如何拆分具有空格和制表符区分的文件行
How to split a file lines with space and tab differentiation?
我有一个以下格式的文件
星期一 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++ 中标记字符串?
来自 boost :
#include <boost/algorithm/string.hpp>
std::vector<std::string> strs;
boost::split(strs, "string to split", boost::is_any_of("t"));
您可以在其中指定任何分隔符。
相关文章:
- 读取制表符分隔的文件时出现问题 C++
- C Visual Studio代码不使用工作区中的单个文件夹编译
- 程序在我的C++工作区中找不到头文件
- 读取以逗号分隔的数字,并将其写入以制表符分隔的文件
- 将制表符分隔的值从文本文件读取到数组中
- 如何将由制表符分隔的文件读入结构
- 预编译的头文件和预编译的二进制文件之间的区别是什么
- 获得有关硬盘扇区中原始数据更改的通知-文件更改通知
- -fprofile使用和-futo配置文件之间的区别是什么
- 写入文件的原始磁盘扇区
- 知道文本文件中的列数,用空格或制表符分隔
- 区分头文件和编译器/语言标准/os/glibc
- 要存储到数据结构中的制表符分隔的文件数据
- 如果我只想要架构良好的工作区,但不一定是分开的编译,如何在C++中组织头/代码文件
- 如何拆分具有空格和制表符区分的文件行
- 当数据包含空格时,如何将制表符分隔文件的内容加载到C++字符串的 2D 向量中
- 仿真过程后.cpp将变量从文件传输到 Matlab 工作区
- C ++中的通用文件对话框和公共项目对话框的主要区别是什么?
- 获取文件中由制表符分隔的所有字符串
- 从制表符分隔的文件中读取2D数据并存储在矢量C++中