CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   How to close a IFstream? (https://www.cfd-online.com/Forums/openfoam-programming-development/71736-how-close-ifstream.html)

fs82 January 13, 2010 08:44

How to close a IFstream?
 
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

l_r_mcglashan January 13, 2010 09:15

Is it not just
Code:

dataStream.close();
?

fs82 January 13, 2010 09:21

No unfortunately not.

dataStream.close() give the following error:

error: ‘class Foam::IFstream’ has no member named ‘close’

kind regards,
Fabian

olesen January 14, 2010 03:58

Quote:

Originally Posted by fs82 (Post 242431)
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


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;

This should handle what you need. If, however, your really want to have a close(), you could also try chaining through with the stdStream() method:
Code:

dataStream.stdStream().close();
But I don't know if there are any side-effects that I've missed there.

fs82 January 14, 2010 04:21

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();
But this doesn't work and leads to a compiler error message.
Thx for your help.

kind regards,
Fabian

olesen January 14, 2010 04:55

Quote:

Originally Posted by fs82 (Post 242546)
Code:

dataStream.stdStream().close();
But this doesn't work and leads to a compiler error message.

On second thought, that of course makes sense.
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.


All times are GMT -4. The time now is 15:34.