How to split a file lines with space and tab differentiation?(如何用空格和制表符区分文件行?)
问题描述
我有一个格式如下的文件
I have a file in below format
1000 年 1 月 1 日星期一(TAB)你好(TAB)你好吗
mon 01/01/1000(TAB)hi hello(TAB)how r you
有没有办法以单独使用 ' ' 作为分隔符(而不是空格)的方式阅读文本?
Is there any way to read the text in such a way to use ' ' alone as delimiter (and not space)?
所以样本输出可以,
1000 年 1 月 1 日星期一
mon 01/01/1000
你好
你好吗
我不能使用 fscanf(),因为它只读取到第一个空格.
I couldn't use fscanf(), since it reads till the first space only.
推荐答案
仅使用标准库设施:
#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)) { // '
' is the default delimiter
std::istringstream iss(line);
std::string token;
while(std::getline(iss, token, ' ')) // but we can specify a different one
tokens.push_back(token);
}
您可以在此处获得更多想法:如何标记一个C++ 中的字符串?
You can get some more ideas here: How do I tokenize a string in C++?
这篇关于如何用空格和制表符区分文件行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何用空格和制表符区分文件行?
基础教程推荐
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- c++ STL设置差异 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
