473,395 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Text File parsing



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
8 7435

"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
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
> 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
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
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

"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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bob | last post by:
Hi, I have a website in a Linux/Apache shared hosting environment and have been given access to the MySQL server running on the same machine. To access this database from PHP, I have to call...
1
by: Scott | last post by:
I am new to perl, and have not found any good examples of parsing to help me out. I have a text file that I am reading into an array that has to be parsed out and put into another file. I have not...
27
by: Eric | last post by:
Assume that disk space is not an issue (the files will be small < 5k in general for the purpose of storing preferences) Assume that transportation to another OS may never occur. Are there...
4
by: Hugh | last post by:
Hello, I am having some problems understanding (most likely), parsing a text file. I would like to parse a file like: block1 { stuff; ... stuffN; };
11
by: .Net Sports | last post by:
In VB.net, I'm trying to do a couple of things in a couple of different blocks of code. I need to take the first 25 characters of a text file, then append at the end some ellipses and a MORE link...
13
by: sonald | last post by:
Hi, Can anybody tell me how to change the text delimiter in FastCSV Parser ? By default the text delimiter is double quotes(") I want to change it to anything else... say a pipe (|).. can anyone...
4
by: thenewuser | last post by:
Hi all, I am working on windows 2000 and using php 5.0 and apache 2.0.59. I am facing a problem while parsing a text file.Actually I am using a pop server for parsing an email.I am downloading...
3
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in...
2
by: flyzone | last post by:
Goodmorning people :) I have just started to learn this language and i have a logical problem. I need to write a program to parse various file of text. Here two sample: --------------- trial...
2
by: python | last post by:
I'm parsing a text file for a proprietary product that has the following 2 directives: #include <somefile> #define <name<value> Defined constants are referenced via <#name#syntax. I'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.