我只想写(追加)到日志文件.我在这里查了一下: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) 我正在尝试打开一个文件输出并附加到它.在附加到它之后,我想将输出位置移动到文件中的其他位置并覆盖现有数据.据我了解,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)