从字符串c ++中提取值

时间:2012-08-12 14:47:05

标签: c++ string ifstream

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

看起来像这样:

  

信息尺寸= 32粗体= 0斜体= 0 unicode = 1 stretchH = 100 smooth = 1 aa = 1 padding = 0,0,0,0
  常用 lineHeight = 32 base = 27 scaleW = 1024 scaleH = 28 pages = 1 packed = 0 alphaChnl = 1
  字符 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
   char id = 37 x = 177 y = 0 width = 19 height = 19 xoffset = -1 yoffset = 8 xadvance = 17 page = 0   CHNL = 15
   char id = 38 x = 216 y = 0 width = 18 height = 19 xoffset = 0 yoffset = 8 xadvance = 18 page = 0 chnl = 15

信息,共同点和基本/全球价值。每个字符行应保存在具有类似格式(x,y,height,offsetX,offsetY ...)的结构的数组(或向量)中

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

提前感谢您的帮助!

3 个答案:

答案 0 :(得分:2)

每一行都以对象类型为前缀 因此,您的读者应该阅读第一个单词,然后决定要阅读的对象。

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

对于每种类型的对象,您需要定义一个用于保存数据的结构。

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

现在你必须定义一个读取数据的方法。在C ++中,我们使用operator>>从流中读取数据。

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

对您需要阅读的其他类型重复此过程。然后写入读取命令变得简单:

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.
}

对其他类型重复此过程。

答案 1 :(得分:0)

important_value=str_from_getline.find(what_to_find);

important_value是一个size_t,它的起点是你在getline()字符串中寻找的东西

答案 2 :(得分:0)

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