LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   parsing text file in C++ and read specific lines (http://www.linuxquestions.org/questions/linux-newbie-8/parsing-text-file-in-c-and-read-specific-lines-4175441668/)

Alkass 12-16-2012 08:06 AM

parsing text file in C++ and read specific lines
 
Hi
My text file looks like
Code:

<block event="1">
..
...
....
</block>

<block event="2">
..
...
....
</block>

So, how would it possible to read in c++ each <block> separately ? I do not want to use any kind of XML parser though - I have something like this bellow, but can get it to work it recursively.



Code:

bool begin_tag = false;
    while (getline(in,line))
    {
        std::string tmp;
        tmp=line;
      if(line.find("<block event=")<=line.size()){
            cout<<line<<endl;
            begin_tag = true;
            continue;
        }
        else if (tmp == "</block>")
        {
            begin_tag = false;

        }

    }
}


johnsfine 12-16-2012 08:35 AM

Quote:

Originally Posted by Alkass (Post 4850350)
how would it possible to read in c++ each <block> separately ?

I can't guess what you mean by that.

What output do you want to produce or what processing are you trying to do?

Quote:

I have something like this bellow, but can get it to work it recursively.
I assume you meant "can't" not "can". But what about it should be recursive?

Are blocks nested? Your example did not show blocks nested.

If you want a non recursive function to process nested blocks, you probably want your variable begin_tag to be a count rather than a bool. It should hold the number of blocks that have started but not completed. If blocks can't nest then that number is just 0 or 1 depending on whether you are reading inside or outside a block. But if blocks can nest then it can go higher than 1.

If you want to recursively process blocks that can nest, you want a function that can be called at the beginning or can be called right after reading a block even, then it reads up to either the end of the block or end of the file. If it sees a nested block, it calls itself to process that nested block.

Alkass 12-16-2012 08:50 AM

My text is like a xml ie each block starts with

Quote:

<block event="1">
and ends with

Quote:

</block>
So, actually the question boils down to this : How can I read-in a text file and print/process one <block> each time , then move to the next <block> etc etc

thanks

-a

johnsfine 12-16-2012 08:58 AM

The same thing happens to me in person when someone says a sentence and mumbles just the most important words, so the meaning of the sentence is lost. If I ask them to say it more clearly, only the filler words are ever pronounced more clearly. The important words are always mumbled just the same the second time as they were the first.

Post #3 of this thread clarifies nothing that was unclear in post #1.

What is insufficient about the code you showed in post #1? What do you want to do that this code cannot do?

Quote:

Originally Posted by Alkass (Post 4850373)
How can I read-in a text file and print/process one <block> each time

I'm not sure that is better than mumbling the same word again. In post #1, the important word "separately" sounds like it should mean something, but it doesn't mean anything specific enough to use as the basis for a helpful answer.

In post #3, you replaced that with "each time". I still can't guess what that might mean in terms of something a program would do.

Alkass 12-16-2012 09:08 AM

I am sorry..I thought was clear what I want to do - From a file containing entries like
Code:

<block event="1">
line1
line2
line3
</block>

<block event="2">
line_a
line_b
line_c
</block>

I need to print the contents of each <block> , so to have something (in C++)
Code:

This is the contents of block number 1
line1
line2
line3

and for the block nbr 2 should return

Code:

This is the contents of block number 2
line_a
line_b
line_c

Is it clear now ? So, actually is like asking how can I print in C++ the lines between two argument,but in my case the lines to be printed are between
Code:

<block event="1">
&
</block>

whilst the event number "1" changes to "2"....etc

thanks

-a


All times are GMT -5. The time now is 03:41 AM.