CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM Running, Solving & CFD (http://www.cfd-online.com/Forums/openfoam-solving/)
-   -   How to open a old file for APPEND (http://www.cfd-online.com/Forums/openfoam-solving/58347-how-open-old-file-append.html)

liu March 27, 2006 18:08

I need to restart the solver a
 
I need to restart the solver at some point. Also I write out some log information for postprocessing.
My question is how can I open the old log file to APPEND new lines?

Now I am using:

//outlet concentration log file
if(!dir(runTime.path()/"outletAlpha")) //if the directory doesn't exist
mkDir(runTime.path()/"outletAlpha");
OFstream outlet1AlphaFile(runTime.path()/"outletAlpha"/"outlet1AlphaLog.dat");

outlet1AlphaFile << bla bla bla



hjasak March 28, 2006 08:33

Have a look at stream handling
 
Have a look at stream handling in C++ - there's a flag you can set wehn opening a stream for append.

Hrv

eugene March 28, 2006 12:00

Google "ifstream C++" //o
 
Google "ifstream C++"


//open file for append
ifstream someStreamName(filename, ios_base::app);

aurelia June 20, 2006 08:51

Hello I used OFstrean myFil
 
Hello
I used
OFstrean myFile(file,ios_base::app)
but get the error "
error: no matching function for call to 'Foam:: OFstream:: OFstream(Foam::fileName&, const std::_Ios_Openmode&)'
/home/aurelia/OpenFOAM/OpenFOAM-1.2/src/OpenFOAM/lnInclude/OFstream.H:107: note: candidates are: Foam:: OFstream:: OFstream(const Foam::fileName&, Foam::IOstream::streamFormat, Foam::IOstream::versionNumber, Foam::IOstream::compressionType)
/home/aurelia/OpenFOAM/OpenFOAM-1.2/src/OpenFOAM/lnInclude/OFstream.H:86: note: Foam:: OFstream:: OFstream(const Foam:: OFstream&)

Should I add something in lnInclude/OFstream.H ?


Thanks
Aurelia

gschaider June 20, 2006 11:47

Problem is that the Foam strea
 
Problem is that the Foam streams (OFstream,IFStream) look a lot like the standard C++ file-streams (ifstream, ofstream) but are not capable of all the things the std-streams are: for instance appending. When you look at the Doxygen info for OFstream you will find that it doesn't take the ios_base-Parameters.

I guess that is because the Foam-streams are capable of orderly output during parallel computations. The std-streams aren't. So if you need appending use the std::ofstream, it should work as well as long as you don't plan to run your program in parallel.

Note: if I am wrong could someone who has actually looked at the IO-code correct me?

hjasak June 20, 2006 17:45

There's still absolutely nothi
 
There's still absolutely nothing stopping you from making a plain old ofstream and working with that. In fact, the streams will swallow the on-the-fly stream modifiers, but the constructor for append is not there. If you'd really like one, I will add it for you.

The additional stuff will sit in /db/IOstreams/Fstreams/OFstream.H and .C and it's pretty straightforward. Not sure what happens with the ogzstreams (compressed data), but there must be a way out...

Hrv

aurelia June 21, 2006 02:20

Bernhard and Hrvoje, thanks fo
 
Bernhard and Hrvoje, thanks for your reply

Hrvoje, if you have time to do it, I will appreciate if you could add this constructor for appending because for me, it is far to be straightforward!

Aurelia

maka June 21, 2006 05:55

I needed the same option. What
 
I needed the same option. What I do is a simple solution. I log the new run in log.2 file and then append it from linux when I finish the simulation (or before) by log.2 >> log.1 linux command.

regards,
Maka.

hjasak June 21, 2006 18:57

OK, it looks like this, using
 
OK, it looks like this, using the stream modifier on construction - the one below opens the file for append:

OFstream of("hello", ios_base::app);
of << 0 << nl << 1 << nl << 2 << endl;


I changed 3 files, all in the main library:

OFstream.H
OFstream.C
regIOobjectWrite.C


You will, of course, need to rebuild the whole thing. The files are attached below http://www.cfd-online.com/OpenFOAM_D...hment_icon.gif hrvsStreams.tgz

Enjoy,

Hrv

aurelia June 22, 2006 04:40

Hrv, Thank you for the files!
 
Hrv,
Thank you for the files! It really helps me.
Aurelia

cedric_duprat November 23, 2007 11:43

Dear forum, I would like al
 
Dear forum,

I would like also to play with OFstream. cause I would like to print every timestep in one file a value of one home-made parameter.
Is there any thing to modified in Hrvoje.tgz for 1.4.1 version of Foam ?
I ask this because I had some error message when compiling ...
It's just to know if I made a mistake some where or, if I have to change something.
I copy the OFstream .C and .H at the right place, samething for regIOobjectWrite.C but, when compiling, I got some error.

Thank you,
Cedric

cedric_duprat November 23, 2007 12:42

Hi again, it's ok now, I for
 
Hi again,
it's ok now, I forgot to add the files in lnInclude :o)
sorry for the disturbance

Cedric

hjasak November 23, 2007 12:53

Well, this is precisely the re
 
Well, this is precisely the reason why we support an open SVN development line: every time you get stuck, you can just go there and see what I've dome to keep the code working between versions.

Enjoy,

Hrv

cedric_duprat November 23, 2007 12:57

well, that's right, I get it f
 
well, that's right, I get it for future problems
Thanks,
Cedric

johndeas November 12, 2008 13:52

Hi, I tried this with openf
 
Hi,

I tried this with openfoam 1.5 :

std::ofstream wallStress
(
runTime.path()/"wallStress",
ios_base::app
);

but got this error message:

channelStress.C:75: error: no matching function for call to 'std::basic_ofstream<char,> >::basic_ofstream(Foam::fileName, const std::_Ios_Openmode&)'
/home/flurec/OpenFOAM/ThirdParty/gcc-4.3.1/platforms/linux/bin/../lib/gcc/i686-p c-linux-gnu/4.3.1/../../../../include/c++/4.3.1/fstream:572: note: candidates are: std::basic_ofstream<_chart,>::basic_ofstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
/home/flurec/OpenFOAM/ThirdParty/gcc-4.3.1/platforms/linux/bin/../lib/gcc/i686-p c-linux-gnu/4.3.1/../../../../include/c++/4.3.1/fstream:556: note: std::basic_ofstream<_chart,>::basic_ofstream() [with _CharT = char, _Traits = std::char_traits<char>]
/home/flurec/OpenFOAM/ThirdParty/gcc-4.3.1/platforms/linux/bin/../lib/gcc/i686-p c-linux-gnu/4.3.1/../../../../include/c++/4.3.1/iosfwd:89: note: std::basic_ofstream<char,> >::basic_ofstream(const std::basic_ofstream<char,> >&)
make: *** [Make/linuxGccDPOpt/channelStress.o] Error 1

Anybody know how to fix this ? I wouldn't want to modify the OFstream source files as proposed in this thread.

markc April 19, 2009 16:09

Hello John,

I had the same struggle. It's because ofstream needs the filename either directly entered or as a pointer variable of type const char*. Well, that's nice to know but I also do not know how to work around it. Maybe someone else?

Brgds,

Mark

albcem July 2, 2009 14:34

Hello Mark,

It seems our paths cross quite often when it comes to problems. In case you do not yet have an answer to your problem, here is a dirty work around I have discovered:

The problem with using ofstream is, as you said, the requirement to pass a "char*" type as file name. But how do you extract "char*" from "word" which we predominantly use in OpenFOAM?

word MyWord = "testing"

stringstream MyStrStream;
char* MyChar;

MyStrStream << MyWord;
MyChar = MyStrStream.str().c_str();

I hope this helps.

Best,

Cem

johndeas September 28, 2009 09:42

Maybe I haven't understood basic things about pointers, but when you create:
Code:

char* MyChar;
and then assign it
Code:

MyStrStream.str().c_str();
aren't you writing in memory chunks you do not control (since no specific amount of memory is allowed to the char*) ?

I also tried this way:
Code:

std::stringstream MyStrStream;
MyStrStream << runTime.path()/"wallStress";

std::ofstream wallStress
(
    MyStrStream.str().c_str(),
    ios_base::app
);

and get that as a result:
Code:

/home/flurec/OpenFOAM/OpenFOAM-1.5/src/OpenFOAM/lnInclude/LListIO.C: In function ‘Foam::Istream& Foam::operator>>(Foam::Istream&, Foam::LList<LListBase, T>&) [with LListBase = Foam::SLListBase, T = Foam::SymmTensor<double>]’:
/home/flurec/OpenFOAM/OpenFOAM-1.5/src/OpenFOAM/lnInclude/LListIO.C:51: internal compiler error: Segmentation fault

I also got a segmentation fault with:
Code:

string MyStrStream(runTime.path()/"wallStress");
   
std::ofstream wallStress
(
    MyStrStream.c_str(),
    ios_base::app
);

So, at that point any help would be appreciated ! Thanks !

jensjo June 14, 2010 03:48

Cannot download the files...
 
HI

I am really interested in the files posted here. But when I am not able to download them... I get a *.unk file instead. Any help?

Kind regards

Jens Johansson
Quote:

Originally Posted by hjasak (Post 181529)
OK, it looks like this, using the stream modifier on construction - the one below opens the file for append:

OFstream of("hello", ios_base::app);
of << 0 << nl << 1 << nl << 2 << endl;


I changed 3 files, all in the main library:

OFstream.H
OFstream.C
regIOobjectWrite.C


You will, of course, need to rebuild the whole thing. The files are attached below http://www.cfd-online.com/OpenFOAM_D...hment_icon.gif hrvsStreams.tgz

Enjoy,

Hrv


Victor June 14, 2010 08:21

I tried it with different versions, but finally it works with

#include <iostream>
using namespace std;
...
fileName myDir = runTime.path();
....

word tracingFileName="TestName"
fileName myFile = myDir/tracingFileName;
...
std:: ofstream myStream(myFile.c_str(),ios_base::app);


I know, there would be an shorter version, but i just copied these lines out of my solver.
I do not know if you really have to include <iostream>, i did not test it!

Victor


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