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

How to close a IFstream?

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

Like Tree1Likes
  • 1 Post By olesen

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   January 13, 2010, 09:44
Default How to close a IFstream?
  #1
Senior Member
 
Dr. Fabian Schlegel
Join Date: Apr 2009
Location: Dresden, Germany
Posts: 222
Rep Power: 17
fs82 is on a distinguished road
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
fs82 is offline   Reply With Quote

Old   January 13, 2010, 10:15
Default
  #2
Senior Member
 
Laurence R. McGlashan
Join Date: Mar 2009
Posts: 370
Rep Power: 23
l_r_mcglashan will become famous soon enough
Is it not just
Code:
dataStream.close();
?
__________________
Laurence R. McGlashan :: Website
l_r_mcglashan is offline   Reply With Quote

Old   January 13, 2010, 10:21
Default
  #3
Senior Member
 
Dr. Fabian Schlegel
Join Date: Apr 2009
Location: Dresden, Germany
Posts: 222
Rep Power: 17
fs82 is on a distinguished road
No unfortunately not.

dataStream.close() give the following error:

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

kind regards,
Fabian
fs82 is offline   Reply With Quote

Old   January 14, 2010, 04:58
Default
  #4
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,677
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by fs82 View Post
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.
anon_q likes this.
olesen is offline   Reply With Quote

Old   January 14, 2010, 05:21
Default
  #5
Senior Member
 
Dr. Fabian Schlegel
Join Date: Apr 2009
Location: Dresden, Germany
Posts: 222
Rep Power: 17
fs82 is on a distinguished road
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
fs82 is offline   Reply With Quote

Old   January 14, 2010, 05:55
Default
  #6
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,677
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by fs82 View Post
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.
olesen is offline   Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
Pingbacks are On
Refbacks are On


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


All times are GMT -4. The time now is 02:44.