Text File parsing

I

Imran

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
 
G

Gernot Frisch

Imran said:
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
}
 
K

Karl Heinz Buchegger

Gernot said:
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;
}
 
G

Gernot Frisch

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
 
K

Karl Heinz Buchegger

Gernot said:
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.
 
M

Marcelo Pinto

Karl Heinz Buchegger said:
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.
 
M

Marcelo Pinto

Imran said:
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.
 
T

Thomas Matthews

Imran said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top