相关疑难解决方法(0)

如何附加到fstream fstream :: app标志的文件似乎不起作用

我只想写(追加)到日志文件.我在这里查了一下:http:
//www.cplusplus.com/reference/iostream/fstream/open/

所以这就是我所做的

#include <fstream>

fstream outfile;

//outfile.open("/tmp/debug.txt" );  // works, simply for writing
outfile.open("/tmp/debug.txt", fstream::app );  // does nothing

outfile << "START" << endl;

outfile.close();
Run Code Online (Sandbox Code Playgroud)

c++ fstream

19
推荐指数
1
解决办法
2万
查看次数

带有std :: ate的std :: ofstream最后没有打开

我正在尝试打开一个文件输出并附加到它.在附加到它之后,我想将输出位置移动到文件中的其他位置并覆盖现有数据.据我了解,std::ios_base::app强制所有写入都在文件的末尾,这不是我想要做的.因此,我认为std::ios_base::ate是传递给它的正确旗帜std::ofstream::open().但是,似乎没有按预期工作:

// g++ test.cpp
// clang++ test.cpp
// with and without -std=c++11
#include <iostream>
#include <fstream>

int main() {
    std::streampos fin, at;
    {
        std::ofstream initial;
        initial.open("test", std::ios_base::out | std::ios_base::binary);
        if ( not initial.good() ) {
            std::cerr << "initial bad open" << std::endl;
            return 1;
        }
        int b = 100;
        initial.write((char*)&b, sizeof(b));
        initial.flush();
        if ( not initial.good() ) {
            std::cerr << "initial write bad" << std::endl;
            return 1;
        } …
Run Code Online (Sandbox Code Playgroud)

c++ fstream c++11

8
推荐指数
1
解决办法
1735
查看次数

标签 统计

c++ ×2

fstream ×2

c++11 ×1