1

我有一个问题,我认为解决方案可能很简单,但我有点卡住了。我有某种配置文件,我试图在 C++ 中解析以获得一些重要的值。

它看起来像这样:

信息大小=32 粗体=0 斜体=0 unicode=1 拉伸H=100 平滑=1 aa=1 填充=0,0,0,0
常用行高=32 基数=27 比例尺W=1024 比例尺H=28 页=1 打包=0 alphaChnl=1
chars count=74
char id=32 x=837 y=15 width=3 height=1 xoffset=-1 yoffset=31 xadvance=8 page=0 chnl=15
char id=33 x=802 y=0 width =4 height=19 xoffset=2 yoffset=8 xadvance=8 page=0 chnl=15
char id=35 x=292 y=0 width=17 height=19 xoffset=0 yoffset=8 xadvance=16 page=0 chnl= 15
字符id=37 x=177 y=0 宽度=19 高度=19 xoffset=-1 yoffset=8 xadvance=17 页面=0 chnl=15
字符id=38 x=216 y=0 width=18 height=19 xoffset=0 yoffset=8 xadvance=18 page=0 chnl=15

Info、common 和 chars 是基本/全局值。每个 char 行都应保存在具有相似格式(x、y、height、offsetX、offsetY ...)的结构的数组(或向量)中

现在我已经尝试 getline() 例如逐行获取每一行,然后使用这些行创建一个 istringstream 以“搜索”这些行以获取我需要的值。值得注意的是,我不需要所有这些值,我需要一种方法来保存每行所需的值。

在此先感谢您的帮助!

4

3 回答 3

2

Each line is prefixed by the type of object.
So your reader should read the first word off and then decide what object to read.

std::ifstream  file("data.txt");
std::string    type;
while(file >> type)
{
         if (type == "info")    { readInfo(file);}
    else if (type == "common")  { readCommon(file);}
    else if (type == "chars")   { readChars(file);}
    else if (type == "char")    { readChar(file);}
}

For each type of object you need to define a structure to hold the data.

// char id=32 x=837 y=15 width=3 height=1 xoffset=-1 yoffset=31 xadvance=8 page=0 chnl=15
struct CharData
{
    int   id;
    int   x;
    int   y;
    int   width;
    int   height;
    int   xoffset;
    int   yoffset;
    int   xadvance;
    int   page;
    int   chnl;
};

Now you have to define a method to read the data. In C++ we use operator>> to read data from a stream.

std::istream& operator>>(std::istream& stream, CharData& data)
{
    // All the data is on one line.
    // So read the whole line (including the '\n')
    std::string        line;
    std::getline(stream, line);

    // convert the single line into a stream for parsing.
    // One line one object just makes it easier to handle errors this way.
    std::stringstream  linestream(line);

    // Assume the prefix type information has already been read (now looks like this)
    // id=32 x=837 y=15 width=3 height=1 xoffset=-1 yoffset=31 xadvance=8 page=0 chnl=15
    std::string   command;
    while(linestream >> command) // This reads one space separated command from the line.
    {
             if (command.substr(0,3) == "id=")        {data.id       = atoi(&command[3]);}
        else if (command.substr(0,2) == "x=")         {data.x        = atoi(&command[2]);}
        else if (command.substr(0,2) == "y=")         {data.y        = atoi(&command[2]);}
        else if (command.substr(0,6) == "width=")     {data.width    = atoi(&command[6]);}
        else if (command.substr(0,7) == "height=")    {data.height   = atoi(&command[7]);}
        else if (command.substr(0,8) == "xoffset=")   {data.xoffset  = atoi(&command[8]);}
        else if (command.substr(0,8) == "yoffset=")   {data.yoffset  = atoi(&command[8]);}
        else if (command.substr(0,9) == "xadvance=")  {data.xadvance = atoi(&command[9]);}
        else if (command.substr(0,5) == "page=")      {data.page     = atoi(&command[5]);}
        else if (command.substr(0,5) == "chnl=")      {data.chnl     = atoi(&command[5]);}
    }
    return stream;
}

Repeat the processes for the other types you need to read. Then writting the read commands becomes simple:

std::vector<CharData>    charVector;
void readChar(std::istream& stream)
{
    CharData     data;
    stream >> data;               // read the object from the stream
                                  // This uses the `operator>>` we just defined above.

    charVector.push_back(data);   // put the data item into a vector.
}

Repeat the processes for the other types.

于 2012-08-12T15:54:07.130 回答
0

important_value=str_from_getline.find(what_to_find);

important_value 是 size_t 并且具有您在 getline() 字符串中寻找的起点

于 2012-08-12T14:52:15.533 回答
0

尝试使用 strtok 或 c++ 正则表达式库

于 2012-08-12T15:51:43.317 回答