问题描述
我正在阅读OpenFoam(v7)(C ++)教程,并遇到了用于IO的以下代码:
// Create a custom directory and write an output file
// Create the output path directory
fileName outputDir = mesh.time().path()/"postProcessing";
// Createe the directory
mkDir(outputDir);
// File pointer to direct the output to
autoptr<OFstream> outputFilePtr;
// Open the file in the newly created directory
outputFilePtr.reset(new OFstream(outputDir/"customOutputFile.dat");
// Write stuff
outputFilePtr() << "# This is a header" << endl;
outputFilePtr() << "0 1 2 3 4 5" << endl;
请有人帮忙解释一下如何打开此文件(我不了解outputFilePtr.reset(new OFstream(outputDir/"customOutputFile.dat");
)
以及如何附加而不是覆盖?添加std :: ios :: app似乎在这里不起作用。
openfoam-v7的构造函数为:
OFstream
(
const fileName& pathname,streamFormat format=ASCII,versionNumber version=currentVersion,compressionType compression=UNCOMpressed,const bool append = false
);
可以找到here 和
141 outputFilePtr.reset( new OFstream(outputDir/"customOutputFile.dat",142 ASCII,143 currentVersion,144 UNCOMpressed,145 true) );
无法出错:在此范围内未声明“ ASCII”, 错误:未在此范围内声明“ currentVersion”,并且 错误:在此范围内未声明“ UNCOMpressed”。
在此先感谢您的帮助。
解决方法
OFstream构造函数:
OFstream(
const fileName& pathname,IOstreamOption streamOpt = IOstreamOption(),const bool append = false
)
所以:
outputFilePtr.reset( new OFstream(outputDir/"customOutputFile.dat",IOstreamOption(),true) );
编辑:对于旧版OpenFOAM的错误,很可能是名称空间问题:
outputFilePtr.reset( new OFstream(outputDir/"customOutputFile.dat",Foam::IOstream::ASCII
Foam::IOstream::currentVersion,Foam::IOstream::UNCOMPRESSED,true) );