|
[Sponsors] |
February 10, 2015, 14:35 |
rewind() function not working
|
#1 |
Super Moderator
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51 |
Hi all,
I open a file to get the lines. After a first check I am going to the second function to store the data but the file can not be re-read due to the fact that I am already at the end of the file (checked with is.eof()). Therefore I checked the Doxygen and get the function "rewind()" which should be used for re-reading the file. This is working: Code:
::read(IFstream& is) { string line; while (is.good()) { is.getLine(line); Info<< "Line: " << is.lineNumber() << " - " << line << endl; is.rewind(); } } Output: Code:
Line: 2 - "EnthalpyDefects" Line: 3 - "EnthalpyDefects" Line: 4 - "EnthalpyDefects" Line: 5 - "EnthalpyDefects" Line: 6 - "EnthalpyDefects" Line: 7 - "EnthalpyDefects" Line: 8 - "EnthalpyDefects" Line: 9 - "EnthalpyDefects" Line: 10 - "EnthalpyDefects" Line: 11 - "EnthalpyDefects" Line: 12 - "EnthalpyDefects" Line: 13 - "EnthalpyDefects" Line: 14 - "EnthalpyDefects" Line: 15 - "EnthalpyDefects" Line: 16 - "EnthalpyDefects" Line: 17 - "EnthalpyDefects" Line: 18 - "EnthalpyDefects" Line: 19 - "EnthalpyDefects" Line: 20 - "EnthalpyDefects" Line: 21 - "EnthalpyDefects" Line: 22 - "EnthalpyDefects" Line: 23 - "EnthalpyDefects" Line: 24 - "EnthalpyDefects" Line: 25 - "EnthalpyDefects" Line: 26 - "EnthalpyDefects" Line: 27 - "EnthalpyDefects" Line: 28 - "EnthalpyDefects" Line: 29 - "EnthalpyDefects" Line: 30 - "EnthalpyDefects" Line: 31 - "EnthalpyDefects" Line: 32 - "EnthalpyDefects" Line: 33 - "EnthalpyDefects" Line: 34 - "EnthalpyDefects" . . . So if I will re-read the file after the first read action like: Code:
::read(IFstream& is) { string line; while (is.good()) { is.getLine(line); Info<< "Line1: " << is.lineNumber() << " - " << line << endl; } //- rewind file for re-read is.rewind(); while (is.good()) { is.getLine(line); Info<< "Line2: " << is.lineNumber() << " - " << line << endl; } } What is my mistake? Thanks in advance, ...
__________________
Keep foaming, Tobias Holzmann |
|
February 11, 2015, 09:15 |
|
#2 |
Senior Member
|
Hi,
rewind is just rdbuf()->pubseekpos(0) and this operation does not modify good/bad state of the stream. So first you read stream until it becomes bad, then you set position but the stream stays bad, so second cycle is just skipped. |
|
February 11, 2015, 09:40 |
|
#3 |
Super Moderator
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51 |
Hi,
thanks for your replay. Sorry for that non quality question. If you go down the doxygen you will find the functions for setting the state of the stream. I did not see them yesterday: Code:
Protected Member Functions inherited from IOstream void setOpened () Set stream opened. More... void setClosed () Set stream closed. More... void setState (ios::iostate state) Set stream state. More... void setGood () Set stream to be good. More... Thank you.
__________________
Keep foaming, Tobias Holzmann |
|
February 11, 2015, 14:01 |
|
#4 |
Super Moderator
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51 |
Hi Alex,
just one question. The setGood() function is protected and I have no access to it using: Code:
is.setGood();
__________________
Keep foaming, Tobias Holzmann |
|
February 11, 2015, 14:55 |
|
#5 |
Senior Member
|
Hi,
No, you can not, at least not directly as Code:
is.setGood() Code:
is.stdStream().clear() 1. http://foam.sourceforge.net/docs/cpp...40afb96a1d9664 2. http://www.cplusplus.com/reference/istream/istream/ for more information. |
|
February 11, 2015, 15:17 |
|
#6 |
Super Moderator
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51 |
Hi alex,
you are right with that I can work but then I can not access the "good() etc" functions because if you set the goodbit with stdStream.clear() you are not modifying the FOAM variables: Code:
if(!is.good()) { Info<< "goodbit: " << is.stdStream().good() << "\n"; Info<< "badbit: " << is.stdStream().bad() << "\n"; Info<< "eofbit: " << is.stdStream().eof() << "\n"; Info<< "failbit: " << is.stdStream().fail() << "\n"; Info<< endl; } if(!is.good()) { Info<< "goodbit: " << is.good() << "\n"; Info<< "badbit: " << is.bad() << "\n"; Info<< "eofbit: " << is.eof() << "\n"; Info<< "failbit: " << is.fail() << "\n"; Info<< endl; } Code:
goodbit: 1 badbit: 0 eofbit: 0 failbit: 0 goodbit: 0 badbit: 0 eofbit: 1 failbit: 1
__________________
Keep foaming, Tobias Holzmann |
|
February 11, 2015, 17:03 |
|
#7 |
Senior Member
|
Well, state of FOAM variables will be updated after the next call to getLine. Here's the example:
Code:
#include "IFstream.H" using namespace Foam; void dump_stream_state(const IFstream& is) { Info<< "---" << endl; Info<< "opened: " << is.opened() << endl; Info<< "closed: " << is.closed() << endl; Info<< "good: " << is.good() << endl; Info<< "eof: " << is.eof() << endl; Info<< "bad: " << is.bad() << endl; Info<< "---" << endl; } int main(int argc, const char **argv) { IFstream is(argv[1]); dump_stream_state(is); string l; do { is.getLine(l); Info<< l << endl; } while(! is.eof()); is.stdStream().clear(); is.rewind(); dump_stream_state(is); is.getLine(l); dump_stream_state(is); is.rewind(); do { is.getLine(l); Info<< l << endl; } while(! is.eof()); return 0; } Code:
alexey at daphne in ~$ IFTestFoam one_line --- opened: 1 closed: 0 good: 1 eof: 0 bad: 0 --- "line" "" --- opened: 1 closed: 0 good: 0 eof: 1 bad: 0 --- --- opened: 1 closed: 0 good: 1 eof: 0 bad: 0 --- "line" "" |
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
channelFoam for a 3D pipe | AlmostSurelyRob | OpenFOAM | 3 | June 24, 2011 13:06 |
ParaView for OF-1.6-ext | Chrisi1984 | OpenFOAM Installation | 0 | December 31, 2010 06:42 |
Problem with rhoSimpleFoam | matteo_gautero | OpenFOAM Running, Solving & CFD | 0 | February 28, 2008 06:51 |
[blockMesh] Axisymmetrical mesh | Rasmus Gjesing (Gjesing) | OpenFOAM Meshing & Mesh Conversion | 10 | April 2, 2007 14:00 |
Droplet Evaporation | Christian | Main CFD Forum | 2 | February 27, 2007 06:27 |