473,322 Members | 1,379 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,322 software developers and data experts.

How to restore istream's state?

Hi,

Suppose, I have the following code and the requirement is written
along with the code. I'm wondering if there is a way to restore the
state of an istream.

int n, m;
std::ifstream file("file.txt");
file >n;

//I want to restore the state of 'file' here.
file >m;//so that m would get the same value as n.

Thanks,
Peng
Jul 23 '08 #1
5 2022
Peng Yu wrote:
Hi,

Suppose, I have the following code and the requirement is written
along with the code. I'm wondering if there is a way to restore the
state of an istream.

int n, m;
std::ifstream file("file.txt");
file >n;

//I want to restore the state of 'file' here.
file >m;//so that m would get the same value as n.
No standard function to do that, but:

int n, m,where;
std::ifstream file("file.txt");
where = file.tellg();
file >n;
file.seekg(where);
file >m;

should be enough for you
Jul 23 '08 #2
On Jul 23, 6:42 pm, Darío Griffo <dario.griffo.lis...@gmail.com>
wrote:
Peng Yu wrote:
Hi,
Suppose, I have the following code and the requirement is written
along with the code. I'm wondering if there is a way to restore the
state of an istream.
int n, m;
std::ifstream file("file.txt");
file >n;
//I want to restore the state of 'file' here.
file >m;//so that m would get the same value as n.

No standard function to do that, but:

int n, m,where;
std::ifstream file("file.txt");
where = file.tellg();
file >n;
file.seekg(where);
file >m;

should be enough for you
This would miss all the error bits and maybe other internal states.
How to get all the internal states variables? Maybe I should keep a
record of them and then restore them completely?

Thanks,
Peng
Jul 24 '08 #3
On Jul 23, 7:19 pm, Peng Yu <PengYu...@gmail.comwrote:
On Jul 23, 6:42 pm, Darío Griffo <dario.griffo.lis...@gmail.com>
wrote:
Peng Yu wrote:
Hi,
Suppose, I have the following code and the requirement is written
along with the code. I'm wondering if there is a way to restore the
state of an istream.
int n, m;
std::ifstream file("file.txt");
file >n;
//I want to restore the state of 'file' here.
file >m;//so that m would get the same value as n.
No standard function to do that, but:
int n, m,where;
std::ifstream file("file.txt");
where = file.tellg();
file >n;
file.seekg(where);
file >m;
should be enough for you

This would miss all the error bits and maybe other internal states.
How to get all the internal states variables? Maybe I should keep a
record of them and then restore them completely?
For example, the following code shows that other state variables are
also important.

#include <sstream>
#include <iostream>
#include <istream>

class istream_restorer {
public:
istream_restorer(std::istream &is);
void doit() const;
private:
mutable std::istream &_is;
mutable std::streampos _where;
};

istream_restorer::istream_restorer(std::istream &is) :
_is(is),_where(_is.tellg()) {
}

void istream_restorer::doit() const {
_is.seekg(_where);
}

int main() {
std::istringstream iss(
"10 9 8 7"
);

istream_restorer isr(iss);

std::string s;
if(getline(iss, s))
std::cout << s << std::endl;
else
std::cout << "Error" << std::endl;

isr.doit();
s = "";
if(getline(iss, s))
std::cout << s << std::endl;
else
std::cout << "Error" << std::endl; // would produce an error here.
}
Jul 24 '08 #4
>>>Suppose, I have the following code and the requirement is written
>>>along with the code. I'm wondering if there is a way to restore the
state of an istream.
http://www.boost.org/doc/libs/1_33_1...ios_state.html
Jul 24 '08 #5
On Jul 24, 1:30 am, Peng Yu <PengYu...@gmail.comwrote:
Suppose, I have the following code and the requirement is
written along with the code. I'm wondering if there is a way
to restore the state of an istream.
int n, m;
std::ifstream file("file.txt");
file >n;
//I want to restore the state of 'file' here.
file >m;//so that m would get the same value as n.
It depends which state you're talking about. Although there's
no standard solution, it's usual to have a class which saves and
restores the formatting state (fmtflags, precision and fill).
There's also a function copyfmt() which copies everything but
the error status and the streambuf pointer; be careful with this
one, however, because it can change the exceptions mask, and
thus trigger an exception.

If you want to reread from the same place, istream::tellg() and
istream::seekg() are a possible solution (but be aware that they
don't work on all types of streams, and may not work on an
ifstream, depending on the file type). You can't, and normally
don't want to save and restore error state; if you're going back
in the file, after having (maybe) seen EOF, just use
ios::clear(). (If the first read succeeded, there was no error
before then.)

There is, IMHO, an error in the standard here. If eofbit is
set, but no other status bit, seekg() should logically clear it,
since the fact that you are at the end of file (but no operation
has actually failed yet) doesn't prevent you from seeking
(although it does mean that a read will fail), and after
seeking, you probably aren't at the end of file anymore. The
standard does allow seekg() to proceed if eofbit is set (unlike
the case with other functions), but doesn't have it resetting
this bit. As a general principle, I would suggest using clear()
before seeking.

Of course, if you really want to restore exactly the initial
state, the simplest solution is to just construct a new
instance.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 24 '08 #6

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

Similar topics

6
by: Jason K | last post by:
Let me preface this by saying this obviously isn't a C++ *language* issue per se; rather probably an issue relating to quality of implementation, unless I'm just misusing iostream... I wrote a...
2
by: DMAC | last post by:
The following failed in EM (or whatever it is called now management studio I think) and I scripted it out and ran it in a query window to get more info. This works fine in SQL 2000 but not in 2005....
4
by: Raquel | last post by:
How are the following two commands different as far as the 'end result' of the restores is concerned: 1. db2 RESTORE DB mydb 2. db2 RESTORE DB mydb TABLESPACE In other words, in the above...
9
by: GL | last post by:
I am running DB2 8.1.1 on AIX 5.1 Having a problem with a redirected restore. Once into the restore continue phase, I immediately get the following “SQL2059W A device full warning was...
11
by: Chris | last post by:
I have searched this group for answers and tried the responses. I am trying to Use an full online backup from our production server and apply it to our test server. The Tablespaces in the...
0
by: Takpol | last post by:
Hello, I have several archived filegroups that have data in them partitioned based on the date. These filegroups have been removed from database after archival. For example two months ago....
0
by: raj.raghavan | last post by:
Hi, I have a database were all the indexes are in a seperate filegroup a few large tables are in a seperate filegroup. We take backup at different times and I have backup of primary, two filegroup...
6
by: Asphalt Blazer | last post by:
Hi, I am doing a redirected restore from an offline backup file. All the paths and files have been created and I managed to do all the set tablespaces commands. But once I do the "restore db...
2
by: david.crow | last post by:
Given the following input file: bob 1 2 3 4 5 mary 2 3 4 5 6 7 susan 3 4 5 6 7 8 9 This code snippet does not read it correctly: class Student {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.