473,789 Members | 2,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

read/write stream - parsing


Hi,
I need to parse a file. This means reading from it as from std::istream.
But - sometimes I also need to put-back some text I read before.
What type of string can I use for that? Something like:
void cLine::Load(std ::istream &data) {
data.ignore(4); // ignore word "line"
char c; // ignore { and } chars
data >> c >> x1 >> y1 >> x2 >> y2 >> c;
}
void Parse(std::istr eam &data) { // <------
cStr token;
data >> token;
if (token == "line") line.Load(data) ;
else if (token == "rect") ....
else ParseError();
}

Syntax of file is for example:
line{1 2 3 4}

Problem is that main Parser must read "line" to know what object to
construct and load, and later load of this object also needs to read same
"line" because this is the syntax.
--
~~~~=~~~~l_;~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~
_|\___J \____, Pozdrawiam, moje www, C++, kontakt, itd.:
X-( ssn256 ) Rafal Maj Raf256 - http://www.raf256.com/me-news/
,"-------------" (strona w budowie)
Jul 22 '05 #1
5 8024
sp**@raf256.com news:Xn******** *************** @213.180.128.20
Syntax of file is for example:
line{1 2 3 4}
Problem is that main Parser must read "line" to know what object to
construct and load, and later load of this object also needs to read
same "line" because this is the syntax.


Hmm.. something like unget() but for entire words...

or perhaps it would be a good idea to use

seekg( -token.size() , ios_base::cur);

? it is compatible when data is in fact an ifstream?

--
~~~~=~~~~l_;~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~
_|\___J \____, Pozdrawiam, moje www, C++, kontakt, itd.:
X-( ssn256 ) Rafal Maj Raf256 - http://www.raf256.com/me-news/
,"-------------" (strona w budowie)
Jul 22 '05 #2
Rafal 'Raf256' Maj wrote:

Hi,
I need to parse a file. This means reading from it as from std::istream.
But - sometimes I also need to put-back some text I read before.
What type of string can I use for that? Something like:

void cLine::Load(std ::istream &data) {
data.ignore(4); // ignore word "line"
char c; // ignore { and } chars
data >> c >> x1 >> y1 >> x2 >> y2 >> c;
}

void Parse(std::istr eam &data) { // <------
cStr token;
data >> token;
if (token == "line") line.Load(data) ;
else if (token == "rect") ....
else ParseError();
}

Syntax of file is for example:
line{1 2 3 4}

Problem is that main Parser must read "line" to know what object to
construct and load
yes
, and later load of this object also needs to read same
"line" because this is the syntax.


No. Why should it expect to parse "line" from the input. That
part of the input has already been parsed and was used to make
the decission which object to create. So when the line parsing
function is called, simply continue with parsing the "{ 1 2 3 4 }"
input.

There is no need to put anything back into the stream. Don't
make the whole thing more complicated then it needs to be.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #3
kb******@gascad .at news:41******** *******@gascad. at
No. Why should it expect to parse "line" from the input. That
part of the input has already been parsed and was used to make
the decission which object to create. So when the line parsing
function is called, simply continue with parsing the "{ 1 2 3 4 }"
input.

There is no need to put anything back into the stream. Don't
make the whole thing more complicated then it needs to be.


Hmm.. Yes, I think that YOu are right, I will use this implementation of
parsing.

Just for curiosity, the seekg thingy will work? (assuming that it works on
stringstram, on on ifstream and the file didnt change in between?)

--
~~~~=~~~~l_;~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~
_|\___J \____, Pozdrawiam, moje www, C++, kontakt, itd.:
X-( ssn256 ) Rafal Maj Raf256 - http://www.raf256.com/me-news/
,"-------------" (strona w budowie)
Jul 22 '05 #4
Rafal 'Raf256' Maj wrote:

kb******@gascad .at news:41******** *******@gascad. at
No. Why should it expect to parse "line" from the input. That
part of the input has already been parsed and was used to make
the decission which object to create. So when the line parsing
function is called, simply continue with parsing the "{ 1 2 3 4 }"
input.

There is no need to put anything back into the stream. Don't
make the whole thing more complicated then it needs to be.


Hmm.. Yes, I think that YOu are right, I will use this implementation of
parsing.

Just for curiosity, the seekg thingy will work? (assuming that it works on
stringstram, on on ifstream and the file didnt change in between?)


Sure it would work.
But what for? You are seeking back in the stream just to read something
from the stream you already know: "line". No need to do that.
--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #5
Rafal 'Raf256' Maj wrote:
Hi,
I need to parse a file.
....
Syntax of file is for example:
line{1 2 3 4}

Problem is that main Parser must read "line" to know what object to
construct and load, and later load of this object also needs to read
same "line" because this is the syntax.


Do yourself a favor and use a parsing framework. See
http://www.boost.org/libs/spirit/index.html for an embedded parser
framework.

Jeff Flinn
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
2532
by: Jason Heyes | last post by:
I want to allow objects of my class to be read from an input stream. I am having trouble with the implementation. Here are the different approaches I have tried: // Version 1.0 - Default constructors class MyClass { Foo foo; // foo and bar require default constructors Bar bar; public:
6
7359
by: Yechezkal Gutfreund | last post by:
I have been using the following code (successfully) to read Xml formated text packets from a TCP stream. The output from the server stream consists of a sequence of well formed Xml documents written to the output stream. We are willing to pay $ to any expert (e.g. MVP) consultant who has to time to help us track down this problem. (we will discuss rates if you can prove your expertise, and problem solving approach).
16
3500
by: ben beroukhim | last post by:
I have huge number of legacy code which use standard files functions. I would like to pass a memory pointer rather than a FILE pointer. I am trying to use FILEs in the code to refer to memory buffers. Basically, I want to be able to use all the standard read and write functions, but I want them to refer to memory locations, rather than disk files. I do not want to touch the legacy code. Does any one know of a library
3
2756
by: Bill Cohagan | last post by:
I'm writing a console app in c# and am encountering a strange problem. I'm trying to use redirection of the standard input stream to read input from a (xml) file. The following code snippet is from this app: =============================== static void Main(string args) { if (args.Length > 0) Console.SetIn(new StreamReader(args)); //executes if I don't use the "<", ">" redirection syntax when invoking XmlTextReader xmlin = new...
5
2262
by: Just Me | last post by:
Using streams how do I write and then read a set of variables? For example, suppose I want to write into a text file: string1,string2,string3 Then read them later. Suppose I want to write and then read: string1, integer1, double1
2
4894
by: microdevsolutions | last post by:
Hello I've seen examples to read a file from somewhere into a HttpWebRequest object then write it to a HttpWebResponse object then stream it into a Stream object, very similar to the following code snipet :- ============================================== Uri uri = new Uri("http://www.somewhere/pdf/image1.pdf"); HttpWebRequest httpRequest = null;
2
2766
by: White Spirit | last post by:
I have a function within an application where a client connected to a server continuously sends data. The code on the server side is of the following form: Socket socket = receiveSocket; NetworkStream networkStream = new NetworkStream (socket); System.IO.StreamReader streamReader = new System.IO.StreamReader (networkStream); while (socket.Connected)
3
1369
by: markclinn | last post by:
I have a device in the field that I access by the stream method. I open the Stream and do the following: 1. stream.write a character to the device. 2. stream.read the information from the device. The problem is that when I am debugging and step through the code there is a long enough pause that I recieve the entire string. If I just run it, I only get the 1st character. I have attached the code below. Public Class Form1 ...
8
2193
by: Bob Bedford | last post by:
Hi all, I've to read and parse an XML file to save the datas in a database. Unfortunately it appens that the datas are wrong. I mean it seems they are not well readed...sometimes only part of it it's ok. It's there any way to read an XML file sequentially using the content instead of a $data = fread($fp, 4096); wich may be the source of the problem ?
0
9663
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
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10195
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9016
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...
0
6765
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4090
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
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.