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++?
这篇关于如何用空格和制表符区分文件行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何用空格和制表符区分文件行?
				
        
 
            
        - DoEvents 等效于 C++? 2021-01-01
 - GDB 不显示函数名 2022-01-01
 - 使用 __stdcall & 调用 DLLVS2013 中的 GetProcAddress() 2021-01-01
 - XML Schema 到 C++ 类 2022-01-01
 - OpenGL 对象的 RAII 包装器 2021-01-01
 - 从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值 2021-01-01
 - 将函数的返回值分配给引用 C++? 2022-01-01
 - 如何提取 __VA_ARGS__? 2022-01-01
 - 哪个更快:if (bool) 或 if(int)? 2022-01-01
 - 将 hdc 内容复制到位图 2022-09-04
 
						
						
						
						
						