CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > OpenFOAM Programming & Development

rewind() function not working

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree2Likes
  • 1 Post By alexeym
  • 1 Post By alexeym

Reply
 
LinkBack Thread Tools Display Modes
Old   February 10, 2015, 15:35
Default rewind() function not working
  #1
Senior Member
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Leoben (Austria)
Posts: 1,100
Blog Entries: 6
Rep Power: 19
Tobi will become famous soon enough
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
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();
     }
}
The code is going back to the first line again and again (will never stop).
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;
     }
}
Now the file is only read once but not twice.
What is my mistake?
Thanks in advance, ...
__________________
Best regards,
Tobias Holzmann

Some interesting OpenFOAM tutorials and videos on www.Holzmann-cfd.de
Tobi is offline   Reply With Quote

Old   February 11, 2015, 10:15
Default
  #2
Senior Member
 
Alexey Matveichev
Join Date: Aug 2011
Location: Nancy, France
Posts: 1,132
Rep Power: 20
alexeym will become famous soon enoughalexeym will become famous soon enough
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.
alexeym is offline   Reply With Quote

Old   February 11, 2015, 10:40
Default
  #3
Senior Member
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Leoben (Austria)
Posts: 1,100
Blog Entries: 6
Rep Power: 19
Tobi will become famous soon enough
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
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...
I think the problem is solved now. I wlil try it today in the evening.
Thank you.
__________________
Best regards,
Tobias Holzmann

Some interesting OpenFOAM tutorials and videos on www.Holzmann-cfd.de
Tobi is offline   Reply With Quote

Old   February 11, 2015, 15:01
Default
  #4
Senior Member
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Leoben (Austria)
Posts: 1,100
Blog Entries: 6
Rep Power: 19
Tobi will become famous soon enough
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
Hi Alex,

just one question. The setGood() function is protected and I have no access to it using:
Code:
is.setGood();
Can I get access to that function or not?
__________________
Best regards,
Tobias Holzmann

Some interesting OpenFOAM tutorials and videos on www.Holzmann-cfd.de
Tobi is offline   Reply With Quote

Old   February 11, 2015, 15:55
Default
  #5
Senior Member
 
Alexey Matveichev
Join Date: Aug 2011
Location: Nancy, France
Posts: 1,132
Rep Power: 20
alexeym will become famous soon enoughalexeym will become famous soon enough
Hi,

No, you can not, at least not directly as

Code:
is.setGood()
You can try to use clear method of std::istream. I.e. use something like

Code:
is.stdStream().clear()
See

1. http://foam.sourceforge.net/docs/cpp...40afb96a1d9664

2. http://www.cplusplus.com/reference/istream/istream/

for more information.
wyldckat likes this.
alexeym is offline   Reply With Quote

Old   February 11, 2015, 16:17
Default
  #6
Senior Member
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Leoben (Austria)
Posts: 1,100
Blog Entries: 6
Rep Power: 19
Tobi will become famous soon enough
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
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;                                     
    }
Output is:
Code:
goodbit: 1
badbit: 0
eofbit: 0
failbit: 0

goodbit: 0
badbit: 0
eofbit: 1
failbit: 1
__________________
Best regards,
Tobias Holzmann

Some interesting OpenFOAM tutorials and videos on www.Holzmann-cfd.de
Tobi is offline   Reply With Quote

Old   February 11, 2015, 18:03
Default
  #7
Senior Member
 
Alexey Matveichev
Join Date: Aug 2011
Location: Nancy, France
Posts: 1,132
Rep Power: 20
alexeym will become famous soon enoughalexeym will become famous soon enough
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;
}
and result:

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"
""
Tobi likes this.
alexeym is offline   Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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 07:42
Problem with rhoSimpleFoam matteo_gautero OpenFOAM Running, Solving & CFD 0 February 28, 2008 07:51
Axisymmetrical mesh Rasmus Gjesing (Gjesing) OpenFOAM Native Meshers: blockMesh 10 April 2, 2007 14:00
Droplet Evaporation Christian Main CFD Forum 2 February 27, 2007 07:27


All times are GMT -4. The time now is 20:51.