|
[Sponsors] |
January 13, 2010, 09:44 |
How to close a IFstream?
|
#1 |
Senior Member
Dr. Fabian Schlegel
Join Date: Apr 2009
Location: Dresden, Germany
Posts: 222
Rep Power: 17 |
Hello,
I have a short question. I wrote my own converter for some field and I opened a file to read in some values: fileName dataFile; laserToFoamDict.lookup("datafile") >> dataFile; IFstream dataStream(dataFile); dataStream >> value; Now after reading I want to close the file. How do I close this file? kind regards, Fabian |
|
January 13, 2010, 10:21 |
|
#3 |
Senior Member
Dr. Fabian Schlegel
Join Date: Apr 2009
Location: Dresden, Germany
Posts: 222
Rep Power: 17 |
No unfortunately not.
dataStream.close() give the following error: error: ‘class Foam::IFstream’ has no member named ‘close’ kind regards, Fabian |
|
January 14, 2010, 04:58 |
|
#4 | |
Senior Member
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,677
Rep Power: 40 |
Quote:
Normally you don't need to explicitly close OpenFOAM streams - just let them go out of scope and let the destructor do it for you. You won't be able to a normal close() method on the IFstream, since the std::istream is private. AFAICT this probably helps with handling both normal and gz files, but it doesn't really matter. The simplest means to handle what you want is something like this small workaround (not tested): Code:
fileName dataFile; ... autoPrr<IFstream> dataStreamPtr(new IFstream(dataFile)); dataStreamPtr() >> value; dataStreamPtr.clear(); // or reuse for the next file ... dataStreamPtr.reset(new IFstream(anotherFile)); dataStreamPtr() >> value2; // finally, via a reference ... dataStreamPtr.reset(new IFstream(yetAnotherFile)); IFstream& dataStream = dataStreamPtr(); dataStream >> value3; Code:
dataStream.stdStream().close(); |
||
January 14, 2010, 05:21 |
|
#5 |
Senior Member
Dr. Fabian Schlegel
Join Date: Apr 2009
Location: Dresden, Germany
Posts: 222
Rep Power: 17 |
Allright, I am more used to the Fortran language and there is good style to close a file after one has finished working with it. I thought I have to do this too in C++, but as you mentioned the destructor will handle this for me. I will accept this. I tried quickly:
Code:
dataStream.stdStream().close(); Thx for your help. kind regards, Fabian |
|
January 14, 2010, 05:55 |
|
#6 | |
Senior Member
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,677
Rep Power: 40 |
Quote:
You'd need to dynamic_cast the std::istream reference returned by the stdStream() method to the std::ifstream reference that it actually is ... if someone really needed to go that route. |
||
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Mesh under region close to wall | Thanos | Siemens | 1 | February 26, 2009 07:02 |
IFstream binary file read type conversion problem | card | OpenFOAM Bugs | 3 | October 30, 2008 15:18 |
Mesh distortion close to cylind wall-GAMBIT 2.2.30 | Mirek | FLUENT | 1 | April 15, 2006 18:22 |
Canvas window does not close | oduor | CFX | 5 | June 12, 2002 22:53 |
[making animations] fclose fails to close files? | Mika | FLUENT | 0 | March 30, 2001 09:19 |