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++?
这篇关于如何用空格和制表符区分文件行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何用空格和制表符区分文件行?
基础教程推荐
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- CString 到 char* 2021-01-01
