0

Possible Duplicate:
Parse config file in C/C++

I have a text file in C++ that looks something like this:

[layer]
type=background
data=
1,1,1,1,1,1,11,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,11,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,11,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,11,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,11,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,11,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,11,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,11,1,1,1,1,1,1,1,1,1,1,1,1,1,

However I have multiple layers in the same text file, and each has to be built in a different way, however I need to get the values shown in "data=" for each layer.

How would I go accomplishing this? A method that I have tried is storing them to a vector, but after storing everything in a vector no sollution to extracting those values from the vector comes to my mind...

while(file >> line)
    {
        words.push_back(line);
    }

    if(find(words.begin(), words.end(), "[header]") != words.end())
    {
        for(int i = find(words.begin(), words.end(), "[header]"); words.at(i) != "\n"; i++)
        {
            word += words.at[i];
        }
    }
    cout << word << endl;
    file.close();
Community
  • 1
  • 1
Bugster
  • 1,552
  • 8
  • 34
  • 56
  • Have you considered using XML? http://xerces.apache.org/xerces-c/ – CharliePrynn Oct 19 '12 at 07:59
  • @Charlie, I think that would be an overkill for such a simple exmaple... – SingerOfTheFall Oct 19 '12 at 08:02
  • 4
    Cause bringing in a full-fledged XML parser and doing DOM stuff makes so much more sense than reading a couple of lines in an ini file...sounds legit – cHao Oct 19 '12 at 08:03
  • 1
    I don't see why using a proper textual data format is overkill. If anything, its good practice. – CharliePrynn Oct 19 '12 at 08:03
  • 4
    I'd agree with you, if XML were a proper textual data format. For a case like this, ini files, JSON, YAML, etc make way more sense. – cHao Oct 19 '12 at 08:04
  • @cHao Care to explain why its not? – CharliePrynn Oct 19 '12 at 08:07
  • 2
    @Charlie, because to use XML you need to mess with the doc structure, the nodes, bring a full-scale parser, etc. You don't build a Gauss cannon when you have to kill a fly, do you?) – SingerOfTheFall Oct 19 '12 at 08:13
  • 2
    @CharliePrynn: One big reason: XML often takes up more space for structure than it does for actual *data*. Any format that needs that much devotion to structure, particularly for something this simple, is pretty ill-suited for the job IMO. – cHao Oct 19 '12 at 08:13
  • Well.. In that case, I guess you shouldn't consider XML. – CharliePrynn Oct 19 '12 at 08:17
  • 1
    You could check other questions about parsing config file formats: e.g. http://stackoverflow.com/questions/1417765/parse-config-file-in-c-c - your requirements are a little different in that you want to extract a vector of numbers: if an existing library doesn't support that, you could do it as a second stage by copying the "1,1,1,1..." value into a string stream then using `char c; int n; for (; my_stream >> n; my_stream >> c && c == ',') v.push_back(n);` – Tony Delroy Oct 19 '12 at 09:24

1 Answers1

0

It's fairly easy. You know that data starts after the "data=" line, and ends with "[layer]" line, so just search for them:

std::ifstream f("your_file");
std::string string;
while( f >> string && !f.eof() )
{
    if( string == "data=")//If we found the "data=" string, we know data begins next.
    {
        std::cout << std::endl << "new layer's data found" << std::endl;
        f >> string;//going to the next string (the actual data)
        //while we don't see the "[layer]" which indicates the end of data...
        while( string != "[layer]"  && !f.eof() )"[layer]"
        {
            std::cout << string;//...we output the data found
            f >> string;//and continue to the next string
        }
    }
}
SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • -1: "It's fairly easy." - to get wrong, apparently. You should check for `[.*]` so you know which section you're in, then scan for data=, and the value need to be extracted to a vector of `int`s. Use of `eof()` checks is wrong - for example - `while (f >> string >> !f.eof())` can read a string successfully and still reach eof (you can test with `echo -n value | app` on Linux/UNIX, but will fail to process that string. – Tony Delroy Oct 19 '12 at 09:19