简体   繁体   English

我如何在此处追加而不是覆盖? (以及此代码如何打开文件?)

[英]How do I append instead of overwrite here? (and how does this code open a file?)

I was going through an OpenFoam (v7) (C++) tutorial and came across this code for IO:我正在阅读 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;

Could somebody please help explain how this file is opened (I don't understand outputFilePtr.reset(new OFstream(outputDir/"customOutputFile.dat"); ) and how to append instead of overwrite? Adding std::ios::app doesn't seem to work here.有人可以帮忙解释一下这个文件是如何打开的(我不明白outputFilePtr.reset(new OFstream(outputDir/"customOutputFile.dat"); )以及如何追加而不是覆盖?添加 std::ios::app 不会似乎在这里不起作用。

The constructor for openfoam-v7 is: openfoam-v7 的构造函数是:

        OFstream
        (
            const fileName& pathname,
            streamFormat format=ASCII,
            versionNumber version=currentVersion,
            compressionType compression=UNCOMPRESSED,
            const bool append = false
        );

which can be found here and可以在这里找到和

141         outputFilePtr.reset( new OFstream(outputDir/"customOutputFile.dat",
142                                 ASCII,
143                                 currentVersion,
144                                 UNCOMPRESSED,
145                                  true) );

fails to error: 'ASCII' was not declared in this scope, error: 'currentVersion' was not declared in this scope and error: 'UNCOMPRESSED' was not declared in this scope.失败错误:'ASCII' 未在此范围内声明,错误:'currentVersion' 未在此范围内声明,错误:'UNCOMPRESSED' 未在此范围内声明。

Many thanks in advance for any help.非常感谢您的帮助。

OFstream constructor: OFstream构造函数:

OFstream(
    const fileName& pathname,
    IOstreamOption  streamOpt = IOstreamOption(),
    const bool      append = false 
)

So:所以:

outputFilePtr.reset( new OFstream(outputDir/"customOutputFile.dat", 
                                  IOstreamOption(),
                                  true) );

Edit: For the error with the old OpenFOAM version, it's most likely a namespace issue:编辑:对于旧 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM