LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-16-2012, 08:06 AM   #1
Alkass
Member
 
Registered: Mar 2010
Posts: 47

Rep: Reputation: 0
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;

        }

     }
}
 
Old 12-16-2012, 08:35 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by Alkass View Post
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.
 
Old 12-16-2012, 08:50 AM   #3
Alkass
Member
 
Registered: Mar 2010
Posts: 47

Original Poster
Rep: Reputation: 0
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
 
Old 12-16-2012, 08:58 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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 View Post
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.

Last edited by johnsfine; 12-16-2012 at 09:07 AM.
 
Old 12-16-2012, 09:08 AM   #5
Alkass
Member
 
Registered: Mar 2010
Posts: 47

Original Poster
Rep: Reputation: 0
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[BASH] How to read multiple lines from a text file? gtirsmiley Programming 13 03-11-2011 10:18 AM
[BASH] How to read multiple lines from a text file? gtirsmiley Programming 1 03-11-2011 06:35 AM
How to show specific lines in a text file in Linux. thamann Linux - Newbie 12 02-03-2011 03:04 PM
Text file manipulation: selecting specific lines/columns using awk and print CHARL0TTE Linux - Newbie 2 02-27-2010 02:40 AM
Read specific lines from a text file chobin Programming 8 06-14-2006 11:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration