[英]How do I append instead of overwrite here? (and how does this code open a file?)
我正在閱讀 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
);
可以在這里找到和
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) );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.