繁体   English   中英

当有同名文件时,我想覆盖文件。 (C ++ file.write)

[英]I want to overwrite a file when there is a file with the same name. (C++ file.write)

我的代码如下所示。 我使用两个缓冲区,因为某种程度上我的缓冲区无法容纳所有数据。 无论如何,我希望每次运行代码时都覆盖我的文件,但是当有一个同名文件时,它不会被覆盖。 因此,我必须在运行之前删除文件。 如何更改代码以覆盖文件?

顺便说一句,我正在用char filename []类型在我的writeFile函数中传递文件名。

ofstream myfile;
myfile.open(filename, ios::out | ios::binary| ios::app);
if (!myfile.is_open()) {
    cout << "cannot open file" << endl;
}
myfile.write((char *)&(*buffer), 512 * 512 * depth);
myfile.write((char *)&(*buffer2), 512 * 512 * depth);

myfile.close();

您的代码按您说的工作。 您必须删除ios::app

ofstream myfile;
myfile.open(filename, ios::out | ios::binary); // <-- changes here
if (!myfile.is_open()) {
    cout << "cannot open file" << endl;
}
myfile.write((char *)&(*buffer), 512 * 512 * depth);
myfile.write((char *)&(*buffer2), 512 * 512 * depth);

myfile.close();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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