如何覆盖当前文件
How can the current file be overwritten?
对于以下代码:
fstream file("file.txt", ios::in):
//some code
//"file" changes here
file.close();
file.clear();
file.open("file.txt", ios::out | ios::trunc);
如何更改最后三行,使当前文件不是关闭的,而是"重新打开"所有内容?
如果我正确理解这个问题,您希望在不关闭文件的情况下清除文件的所有内容(即通过设置EOF位置将文件大小设置为0)。据我所知,你提出的解决方案是最吸引人的。
您的另一个选择是使用特定于操作系统的函数来设置文件的末尾,例如,在Windows上的SetEndOfFile()或在POSIX上的truncate()。
如果你只想从文件的开头开始写作,Simon的解决方案是可行的。在不设置文件结尾的情况下使用它可能会使您处于这样一种情况,即您的垃圾数据超过了您编写的最后一个位置。
您可以倒带文件:将放置指针放回文件的开头,这样下次您写东西时,它将覆盖文件的内容。为此,您可以像这样使用seekp
:
fstream file("file.txt", ios::in | ios::out); // Note that you now need
// to open the file for writing
//some code
//"something" changes here
file.seekp(0); // file is now rewinded
请注意,它不会擦除任何内容。只有当您覆盖它时,请小心。
我猜您正试图避免传递"file.txt"参数,并试图实现类似的东西
void rewrite( std::ofstream & f )
{
f.close();
f.clear();
f.open(...); // Reopen the file, but we dont know its filename!
}
然而,ofstream
没有提供底层流的文件名,也没有提供清除现有数据的方法,所以你有点运气不好。(它确实提供了seekp
,它可以让您将写光标放回文件的开头,但不会截断现有内容…)
我要么只将文件名传递给需要它的函数
void rewrite( std::ostream & f, const std::string & filename )
{
f.close();
f.clear();
f.open( filename.c_str(), ios::out );
}
或者将文件流和文件名打包到一个类中。
class ReopenableStream
{
public:
std::string filename;
std::ofstream f;
void reopen()
{
f.close();
f.clear();
f.open( filename.c_str(), ios::out );
}
...
};
如果你觉得过于热情,你可以让ReopenableStream
实际上表现得像一个流,这样你就可以写reopenable_stream<<foo;
而不是reopenable_stream.f<<foo
,但IMO似乎有些过头了。
相关文章:
- 使用ios:ate写入到流会覆盖现有文件
- 如何在 c++ 中覆盖相同的文本文件
- 每个文件(理想情况下是每个部分)的 clang 格式样式覆盖
- 在多个头文件中从接口声明被覆盖的函数时,如何避免重复代码?
- lnk1114无法覆盖原始文件"xxx.lib",错误代码32
- std :: ofstream,覆盖错误的文件行
- 覆盖现有的文本文件C
- 使用 std::cout 将"\r"写入文件会保存以前覆盖的行
- C++继承 - 覆盖功能,包括使用 "::" s、.h 文件和.cpp文件
- 如何在用户输入值后写入.csv文件的下一行,而不会覆盖以前的值?
- 需要在后代 .h 文件中重新声明被覆盖的函数
- 如何使用文件系统minifiter在网络共享驱动器上阻止文件覆盖
- 如何在标题文件中获取成员功能的正确代码覆盖范围
- C++删除或覆盖文件中的现有信息
- 覆盖生成文件中的宏常量
- 使用 tellg() 和 seekp() 覆盖文件
- C/C++ Linux 共享库能否通过将磁盘上自己的文件覆盖到较新版本来自动更新
- R Makevars 文件覆盖 R CMD 的默认 g++ 选项?
- 输出文件覆盖不正确
- 生成文件覆盖默认隐式规则