如何解析未知大小的数据
How to parse this data of unknown size
我有一个简单的文本文件,每行包含指令。如
A 1 1
B 2 1 A
C 3 1 A
D 4 1 B C
基本语法是Letter, Num, Num, Letter(s)
我只是不知道我应该调用什么函数来解析数据,以及如何在给定的语法中解析它。我觉得有很多方法可以做到。
下面的c++示例显示了从文件中读取单个字符的一种可能方法,控制行尾:
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
int main(void)
{
ifstream inpFile("test.txt");
string str;
char c;
while (inpFile.good()) {
// read line from file
getline(inpFile, str);
// make string stream for reading small pieces of data
istringstream is(str);
// read data ingnoring spaces
do
{
is >> c; // read a single character
if (!is.eof()) // after successful reading
cout << c << " "; // output this character
} while (is.good()); // control the stream state
cout << "[End of line]" << endl;
}
cout << "[End of file]" << endl;
}
这里istringstream
用于处理getline
得到的单行。
读取is >> c
值为c
的字符后可检查内容,例如:
if (!is.eof()) // after successful reading
{
// analyze the content
if ( isdigit(c) )
cout << (c - '0') << "(number) "; // output as a digit
else
cout << c << "(char) "; // output as a non-number
}
注意:如果文件可以包含的不是单个字符/数字,而是数字和单词,c
的类型应该是合适的(例如string
)
在c++中,读取整行并从中生成一个流,然后使用>>
从该流中读取。
的例子:
std::ifstream file(filename);
std::string line;
while (file.getline(line))
{
std::istringstream in(line);
char letter;
int number1;
int number2;
std::vector<char> letters;
if (in >> letter >> number1 >> number2)
{
char letter2;
while (in >> letter2)
{
letters.push_back(letter2);
}
}
}
这是一个C语言的例子,读取行,然后从开始(使用指针)输出可读字符(代码大于32):
#include <stdio.h>
#include <ctype.h>
#define MAX_LINE_LEN 80
int main(void)
{
FILE * inpFile = fopen("test.txt", "r");
char buf[MAX_LINE_LEN];
char *p;
while (!feof(inpFile))
{
// read a line from file
if (fgets(buf, MAX_LINE_LEN, inpFile) != NULL)
{
p = buf; // start from the beginning of line
// reading data from string till the end
while (*p != 'n' && *p != ' ')
{
// skip spaces
while (isspace(*p) && *p != 'n') p++;
if (*p > 32)
{
// output character
printf("%c ", *p);
// move to next
p++;
}
}
}
printf("[End of line]n");
}
printf("[End of file]n");
return 0;
}
要从行中提取数字和单词,可以这样做:
// reading data from string till the end
while (*p != 'n' && *p != ' ')
{
// skip spaces
while (isspace(*p) && *p != 'n') p++;
if (*p > 32)
{
int num;
char word[MAX_LINE_LEN];
// trying to read number
if (sscanf(p, "%i", &num))
{
printf("%i(number) ", num);
}
else // read string
{
sscanf(p, "%s", word);
printf("%s(string) ", word);
}
// move to next space in the simplest way
while (*p > 32) p++;
}
}
相关文章:
- 如何解析表示树状数据结构的字符串
- 将文本文件解析为树状数据结构
- 使用 Boost.Spirit 解析具有混合数据类型的 OBJ 文件?
- 在 c++ 中解析数据包数据的最佳方法是什么?
- 当我使用 QDomDocument 解析 html 数据时,如何保留 "?
- C++控制台:解析 METAR 数据
- 将特定字符串数据解析为变量
- 如何将 JSON 键值(我使用 cpp boost 解析)存储在相关数据结构中?
- 如何使用 ZeroMQ 在特定端口上监听和解析 UDP 数据?
- 在 Ubuntu 中解析数据的速度比 Windows 10 快
- 如何通过解析缓冲区并将数据放入正确的结构来处理传入的数据包连接?
- Python API用于解析包含数组格式的protobuf数据的二进制文件
- 使用 C++ 解析 URL 中的 JSON 数据
- 如何为C++结构自动生成 python 数据解析器
- 如何解析 KLV 数据
- 将内存保存到文件并加载它而不必解析数据?
- 如何使用 cpprestsdk 从websocket_client解析 json 数据
- 网络数据包解析类设计
- 如何通过数据包解析在FTP上传/下载中获得源和目的地的完整路径
- C++读取大数据,解析,然后写入数据