如何解析未知大小的数据

How to parse this data of unknown size

本文关键字:数据 何解析 未知      更新时间:2023-10-16

我有一个简单的文本文件,每行包含指令。如

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++;
            }
        }