473,666 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

someotherdatahe re
####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 7451

"Imran" <im******@in.bo sch.com> schrieb im Newsbeitrag
news:cf******** **@ns1.fe.inter net.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

someotherdatahe re
####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.tx t"); // 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.tx t"); // 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******@gasca d.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.bo sch.com> wrote in message
news:cf******** **@ns1.fe.inter net.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.bo sch.com> wrote in message news:<cf******* ***@ns1.fe.inte rnet.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

someotherdatahe re
####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

someotherdatahe re
####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.l earn.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
6968
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 mysql_connect(host, user, password) where the password is hardcoded into my PHP source file in clear text. I see two security problems with this:
1
17521
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 found any good examples of this. Does anyone know of any? For example the text file looks like this... 900002 "Test,Test" 1/1/2004 F 21 with tabs inbetween each of the colums of the text file.
27
4928
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 any solid reasons to prefer text files over binary files files?
4
1721
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
2813
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 to a webpage where viewers can read the rest of the article: This is the first few characters of text from my file.......<a href="article-to-read.aspx"> MORE </a> ...I also need to do some in file parsing where I start at one known keyword (START...
13
4955
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 please tell me how do i go about it?
4
1658
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 new mails from that server using php and parsing the attachments.I want to parse text files as well. BUt when i save the attached .txt file on my machine, "=20" gets appended at the end of every line. If the file is an xml file, sometimes "=90" gets...
3
4375
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 the file) with the location. And for a particular section I parse only that section. The file is something like, .... DATAS
2
2142
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 text bla bla bla bla error bla bla bla bla bla bla bla bla on more lines
2
2586
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 looking for a single text stream that results from processing a file containing these directives. Even better would be an iterator(?) type
0
8448
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8871
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8640
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7387
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2773
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.