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

How to open a old file for APPEND

Register Blogs Community New Posts Updated Threads Search

Like Tree3Likes

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 27, 2006, 18:08
Default I need to restart the solver a
  #1
liu
Senior Member
 
Xiaofeng Liu
Join Date: Mar 2009
Location: State College, PA, USA
Posts: 118
Rep Power: 17
liu is on a distinguished road
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


__________________
Xiaofeng Liu, Ph.D., P.E.,
Assistant Professor
Department of Civil and Environmental Engineering
Penn State University
223B Sackett Building
University Park, PA 16802


Web: http://water.engr.psu.edu/liu/
liu is offline   Reply With Quote

Old   March 28, 2006, 08:33
Default Have a look at stream handling
  #2
Senior Member
 
Hrvoje Jasak
Join Date: Mar 2009
Location: London, England
Posts: 1,905
Rep Power: 33
hjasak will become famous soon enough
Have a look at stream handling in C++ - there's a flag you can set wehn opening a stream for append.

Hrv
__________________
Hrvoje Jasak
Providing commercial FOAM/OpenFOAM and CFD Consulting: http://wikki.co.uk
hjasak is offline   Reply With Quote

Old   March 28, 2006, 12:00
Default Google "ifstream C++" //o
  #3
Senior Member
 
Eugene de Villiers
Join Date: Mar 2009
Posts: 725
Rep Power: 21
eugene is on a distinguished road
Google "ifstream C++"


//open file for append
ifstream someStreamName(filename, ios_base::app);
eugene is offline   Reply With Quote

Old   June 20, 2006, 08:51
Default Hello I used OFstrean myFil
  #4
New Member
 
Aurelia Cure
Join Date: Mar 2009
Location: Lund, Sweden
Posts: 18
Rep Power: 17
aurelia is on a distinguished road
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
aurelia is offline   Reply With Quote

Old   June 20, 2006, 11:47
Default Problem is that the Foam strea
  #5
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
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?
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request
gschaider is offline   Reply With Quote

Old   June 20, 2006, 17:45
Default There's still absolutely nothi
  #6
Senior Member
 
Hrvoje Jasak
Join Date: Mar 2009
Location: London, England
Posts: 1,905
Rep Power: 33
hjasak will become famous soon enough
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
__________________
Hrvoje Jasak
Providing commercial FOAM/OpenFOAM and CFD Consulting: http://wikki.co.uk
hjasak is offline   Reply With Quote

Old   June 21, 2006, 02:20
Default Bernhard and Hrvoje, thanks fo
  #7
New Member
 
Aurelia Cure
Join Date: Mar 2009
Location: Lund, Sweden
Posts: 18
Rep Power: 17
aurelia is on a distinguished road
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
aurelia is offline   Reply With Quote

Old   June 21, 2006, 05:55
Default I needed the same option. What
  #8
Senior Member
 
Maka Mohu
Join Date: Mar 2009
Posts: 305
Rep Power: 18
maka is on a distinguished road
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.
maka is offline   Reply With Quote

Old   June 21, 2006, 18:57
Default OK, it looks like this, using
  #9
Senior Member
 
Hrvoje Jasak
Join Date: Mar 2009
Location: London, England
Posts: 1,905
Rep Power: 33
hjasak will become famous soon enough
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 hrvsStreams.tgz

Enjoy,

Hrv
__________________
Hrvoje Jasak
Providing commercial FOAM/OpenFOAM and CFD Consulting: http://wikki.co.uk
hjasak is offline   Reply With Quote

Old   June 22, 2006, 04:40
Default Hrv, Thank you for the files!
  #10
New Member
 
Aurelia Cure
Join Date: Mar 2009
Location: Lund, Sweden
Posts: 18
Rep Power: 17
aurelia is on a distinguished road
Hrv,
Thank you for the files! It really helps me.
Aurelia
aurelia is offline   Reply With Quote

Old   November 23, 2007, 10:43
Default Dear forum, I would like al
  #11
Senior Member
 
Cedric DUPRAT
Join Date: Mar 2009
Location: Nantes, France
Posts: 195
Rep Power: 17
cedric_duprat is on a distinguished road
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 is offline   Reply With Quote

Old   November 23, 2007, 11:42
Default Hi again, it's ok now, I for
  #12
Senior Member
 
Cedric DUPRAT
Join Date: Mar 2009
Location: Nantes, France
Posts: 195
Rep Power: 17
cedric_duprat is on a distinguished road
Hi again,
it's ok now, I forgot to add the files in lnInclude :o)
sorry for the disturbance

Cedric
cedric_duprat is offline   Reply With Quote

Old   November 23, 2007, 11:53
Default Well, this is precisely the re
  #13
Senior Member
 
Hrvoje Jasak
Join Date: Mar 2009
Location: London, England
Posts: 1,905
Rep Power: 33
hjasak will become famous soon enough
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
__________________
Hrvoje Jasak
Providing commercial FOAM/OpenFOAM and CFD Consulting: http://wikki.co.uk
hjasak is offline   Reply With Quote

Old   November 23, 2007, 11:57
Default well, that's right, I get it f
  #14
Senior Member
 
Cedric DUPRAT
Join Date: Mar 2009
Location: Nantes, France
Posts: 195
Rep Power: 17
cedric_duprat is on a distinguished road
well, that's right, I get it for future problems
Thanks,
Cedric
cedric_duprat is offline   Reply With Quote

Old   November 12, 2008, 12:52
Default Hi, I tried this with openf
  #15
Senior Member
 
John Deas
Join Date: Mar 2009
Posts: 160
Rep Power: 17
johndeas is on a distinguished road
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.
johndeas is offline   Reply With Quote

Old   April 19, 2009, 16:09
Default
  #16
Senior Member
 
Mark Couwenberg
Join Date: Mar 2009
Location: Netherlands
Posts: 130
Rep Power: 17
markc is on a distinguished road
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
markc is offline   Reply With Quote

Old   July 2, 2009, 14:34
Default
  #17
Member
 
Cem Albukrek
Join Date: Mar 2009
Posts: 52
Rep Power: 17
albcem is on a distinguished road
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
albcem is offline   Reply With Quote

Old   September 28, 2009, 09:42
Default
  #18
Senior Member
 
John Deas
Join Date: Mar 2009
Posts: 160
Rep Power: 17
johndeas is on a distinguished road
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 !
johndeas is offline   Reply With Quote

Old   June 14, 2010, 03:48
Default Cannot download the files...
  #19
New Member
 
Jens Johansson
Join Date: Mar 2009
Posts: 2
Rep Power: 0
jensjo is on a distinguished road
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 View Post
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 hrvsStreams.tgz

Enjoy,

Hrv
jensjo is offline   Reply With Quote

Old   June 14, 2010, 08:21
Default
  #20
New Member
 
Victor Fleischer
Join Date: Nov 2009
Posts: 21
Rep Power: 16
Victor is on a distinguished road
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
ayhan515 and waiter120 like this.
Victor is offline   Reply With Quote

Reply


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
how to open a cas file in gambit Michal K FLUENT 5 March 14, 2009 16:37
FORTRAN(OPEN->APPEND) kris Siemens 1 November 30, 2004 07:57
where to open the file jx FLUENT 0 February 19, 2004 12:19
Scheme: append to existend file Ingolf FLUENT 0 November 10, 2003 05:27
append data to a file Wen Long Main CFD Forum 1 April 11, 2002 20:45


All times are GMT -4. The time now is 03:30.