By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,929 Members | 1,284 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,929 IT Pros & Developers. It's quick & easy.

Text File parsing

P: n/a


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
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a

"Imran" <im******@in.bosch.com> schrieb im Newsbeitrag
news:cf**********@ns1.fe.internet.bosch.com...


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
}

Jul 22 '05 #2

P: n/a
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
kb******@gascad.at
Jul 22 '05 #3

P: n/a
> 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
Jul 22 '05 #4

P: n/a
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
kb******@gascad.at
Jul 22 '05 #5

P: n/a
Karl Heinz Buchegger <kb******@gascad.at> wrote in message news:<41***************@gascad.at>...
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.
Jul 22 '05 #6

P: n/a

"Imran" <im******@in.bosch.com> wrote in message
news:cf**********@ns1.fe.internet.bosch.com...


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
Jul 22 '05 #7

P: n/a
"Imran" <im******@in.bosch.com> wrote in message news:<cf**********@ns1.fe.internet.bosch.com>...
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.
Jul 22 '05 #8

P: n/a
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

Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.