从文件读入.多个分隔符?C++
Reading In From A File. Multiple Delimiters? C++
所以我试图从文件中读取一些信息,但我很难做到这一点。这是我尝试读入的文件的格式。
Name#Description
Name2#Description2 ...
NameX#DescriptionX
%
info1,info2,info3,info4 ...
infoX1,infoX2,infoX3,infoX4,
%
Keyword#Message
所以我要做的是在每次#
后分解代码的第一部分,并将 Name 拆分为一个变量,将描述拆分为另一个变量。然后在第一个%
之后,将每条信息拆分为它自己的变量。最后,在第二个%
之后,将它们按#
拆分,就像上面一样,但变量不同。
所以我尝试while(getline(inFile, str, '%')
但它在第一行后停止阅读,然后转到%
下的任何内容。在这里遇到一些麻烦,所以任何帮助将不胜感激!
下面是一个(不完整的)结构,用于执行此操作:
#include <fstream>
#include <iostream>
#include <string>
int main(void) {
const char* in_file_path = "foo.txt";
std::ifstream in_file(in_file_path);
std::string buffer = "";
while (std::getline(in_file, buffer)) {
//parse input using `#` as separator...
if (buffer.at(0) == '%') {
// get position of `%` char in stream using (e.g.) in_file.seekg
break;
}
}
// after getting position of first `%` char in stream, this loop
// will start from this position in the file
while (std::getline(in_file, buffer)) {
// parse input using `,` as separator...
if (buffer.at(0) == `%`) {
// get position of `%` char in stream using (e.g.) in_file.seekg
break;
}
}
// ... and so on for as many separations as you need until you hit end of file
}
这依赖于使用(例如)seekg
std::ifstream
方法:
http://en.cppreference.com/w/cpp/io/basic_istream/seekg
相关文章:
- 当用户键入分隔符时,停止getline()输入
- 为什么在逗号分隔符上下文中将预增量的结果强制转换为void
- 如何在 c++ 中使用 ',' 作为 getline 分隔符
- 带有多个字符分隔符的正则表达式
- 使用 char 分隔符解析C++中的字符串,但将可重复的字符保留为每个解析的子字符串 (C++ STL) 中的分隔符
- 字符串开头的分隔符
- 尝试将 c 字符串数组与分隔符连接起来
- 正则表达式以匹配数字的重复模式,后跟任何类型的分隔符?
- 根据新的行分隔符从字符串中删除子字符串
- 同一分隔符之间的多个子字符串
- 通过分隔符分隔包含 UTF-16 BE 文本的uint8_t数组
- 字符串流分隔符
- 如何在 std::getline 中自定义分隔符
- getline() 上的分隔符无法正常工作
- 示例代码中使用分隔符将 std::string 拆分为 std::vector 的范围问题
- C++使用分隔符读取文件
- 使用连续分隔符和空最后一个字符进行拆分
- 到达分隔符时跳到文本文件的下一行
- 使用空格分隔符从文本文件将对象读入数组
- 使用正则表达式c++从单词和分隔符之间的字符串中提取所有子字符串