Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Text File parsing

Reply
Thread Tools

Text File parsing

 
 
Imran
Guest
Posts: n/a
 
      08-11-2004


hello all, I have to parse a text file and get some value in that.

text file content is as follows.

####TEXT FILE CONTENT STARTS HERE #####
/start
first
0x1234
AC
/end

/start
first
0x12345
AC
/end

/start
first
0x12344
AC
/end

someotherdatahere
####TEXT FILE CONTENT ENDSHERE #####

If user gives "first" to my program, I have to give him 0x1234. So my doubt
is, how can I parse text files in C++.

And in text file, I have to serach in /start and /end block.
Thanks in Adv


 
Reply With Quote
 
 
 
 
Gernot Frisch
Guest
Posts: n/a
 
      08-11-2004

"Imran" <> schrieb im Newsbeitrag
news:cfckk8$kse$...
>
>
> hello all, I have to parse a text file and get some value in that.
>
> text file content is as follows.
>
> ####TEXT FILE CONTENT STARTS HERE #####
> /start
> first
> 0x1234
> AC
> /end
>
> /start
> first
> 0x12345
> AC
> /end
>
> /start
> first
> 0x12344
> AC
> /end
>
> someotherdatahere
> ####TEXT FILE CONTENT ENDSHERE #####
>
> If user gives "first" to my program, I have to give him 0x1234. So

my doubt
> is, how can I parse text files in C++.
>
> And in text file, I have to serach in /start and /end block.
> Thanks in Adv


ifstream is("filename.txt"); // open a file stream

string line; // S string for a line to read
while(is.good()) // As long as there's data
{
is >> line; // read a line
cout << line << endl; // output it
}



 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      08-11-2004
Gernot Frisch wrote:
>
>
> ifstream is("filename.txt"); // open a file stream
>
> string line; // S string for a line to read
> while(is.good()) // As long as there's data
> {
> is >> line; // read a line
> cout << line << endl; // output it
> }


Not a good idea. The typical question with code like this
is: "Why is the last word processed twice?"

A stream goes into a fail state (such as eof) only until
you try AND fail to read past the end of file. Thus the
above loop will have undefined behaviour when is >> line
fails the first time (usually at eof). The read operation
fails and yet you process it as if nothing has happened.

So at least it has to read

while( is.good() )
{
is >> line;
if( is.good() )
cout << line << endl;
}

The usual idiom in C++ is

while( data can be read ) {
do something with the read data
}

if( stream is not in eof state )
there was an error during read
else
all data could be read correctly

------

while( is >> line ) {
cout << line << endl;
}

if( !is.eof() ) {
cout << "There was an error during read\n";
return;
}



--
Karl Heinz Buchegger

 
Reply With Quote
 
Gernot Frisch
Guest
Posts: n/a
 
      08-11-2004
> while( is >> line ) {
> cout << line << endl;
> }
>
> if( !is.eof() ) {
> cout << "There was an error during read\n";
> return;
> }


Thank you, I didn't know. I use fopen or CreateFile in an own class.
-Gernot


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      08-11-2004
Gernot Frisch wrote:
>
> > while( is >> line ) {
> > cout << line << endl;
> > }
> >
> > if( !is.eof() ) {
> > cout << "There was an error during read\n";
> > return;
> > }

>
> Thank you, I didn't know. I use fopen or CreateFile in an own class.


It's the same issue, C++ took over this behaviour from C.

(In a nutshell: Neither C nor C++ try to guess what the next
input operation will do. Only after that operation is done
it is known if it failed. Note that this is eg. different
to PASCAL, where eof becomes true after the last record
from a file has been read. Thus in PASCAL programs you
often see
while( not eof() ) do begin
read
process
end

But C and C++ are different. eof becomes true only after
an attempt to read past the end of file and not when the
last data from the file has been read.

--
Karl Heinz Buchegger

 
Reply With Quote
 
Marcelo Pinto
Guest
Posts: n/a
 
      08-11-2004
Karl Heinz Buchegger <> wrote in message news:<>...
> while( is >> line ) {
> cout << line << endl;
> }
>
> if( !is.eof() ) {
> cout << "There was an error during read\n";
> return;
> }


Note that if the line in the file has some space or tab, it wouldn't
be fully read into the line variable.

Thats why I always use:
while (getline(is, line))
{
//process the line read
}

if (!is.eof())
{
//the input wasn't fully read
}

Best regards,

Marcelo Pinto.
 
Reply With Quote
 
Jeff Flinn
Guest
Posts: n/a
 
      08-11-2004

"Imran" <> wrote in message
news:cfckk8$kse$...
>
>
> hello all, I have to parse a text file and get some value in that.


....

> If user gives "first" to my program, I have to give him 0x1234. So my

doubt
> is, how can I parse text files in C++.


See http://www.boost.org/libs/spirit/index.html

Jeff F


 
Reply With Quote
 
Marcelo Pinto
Guest
Posts: n/a
 
      08-11-2004
"Imran" <> wrote in message news:<cfckk8$kse$>...
> hello all, I have to parse a text file and get some value in that.
>
> text file content is as follows.
>
> ####TEXT FILE CONTENT STARTS HERE #####
> /start
> first
> 0x1234
> AC
> /end
>
> /start
> first
> 0x12345
> AC
> /end
>
> /start
> first
> 0x12344
> AC
> /end
>
> someotherdatahere
> ####TEXT FILE CONTENT ENDSHERE #####
>
> If user gives "first" to my program, I have to give him 0x1234. So my doubt
> is, how can I parse text files in C++.
>
> And in text file, I have to serach in /start and /end block.
> Thanks in Adv


To solve this problem I would use a state machine:

</start>
idle ---------> processing ---\
^ |
| </end> |
\----------------------------/

The idle class does nothing to the input unless it encounters a /start
when it transfers control to the processing class which is responsible
for processing the input. When the processing class encounters a /end
it transfers control back to the idle class. (read the GoF pattern
that deals with state machines)

Note that the processing class may be more than one class. Your
"example file" suggest that it would be necessary to have four
diferent classes to do the processing one for each line of your
"register".

Good luck,

Marcelo Pinto.
 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      08-12-2004
Imran wrote:
> hello all, I have to parse a text file and get some value in that.
>
> text file content is as follows.
>
> ####TEXT FILE CONTENT STARTS HERE #####
> /start
> first
> 0x1234
> AC
> /end
>
> /start
> first
> 0x12345
> AC
> /end
>
> /start
> first
> 0x12344
> AC
> /end
>
> someotherdatahere
> ####TEXT FILE CONTENT ENDSHERE #####
>
> If user gives "first" to my program, I have to give him 0x1234. So my doubt
> is, how can I parse text files in C++.
>
> And in text file, I have to serach in /start and /end block.
> Thanks in Adv
>
>


How do you know which block to pull the information out of?
Looks like a bad or poorly constructed data file.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SAX parsing problem, when element contains text like "[text]" Kai Schlamp Java 1 03-27-2008 08:36 PM
In file parsing, taking the first few characters of a text file after a readfile or streamreader file read... .Net Sports ASP .Net 11 01-17-2006 12:44 AM
Assistance parsing text file using Text::CSV_XS Domenico Discepola Perl Misc 6 09-02-2004 03:55 PM
SAX Parsing - Weird results when parsing content between tags. Naren XML 0 05-11-2004 07:25 PM
Perl expression for parsing CSV (ignoring parsing commas when in double quotes) GIMME Perl 2 02-11-2004 05:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57