使用 std::ofstream 仅重写前 30 个字符的方法
Way to rewrite only first 30 chars using std::ofstream?
有没有办法使用 std::ofstream 只重写文本文件的前 30 个字符,而不清除文本文件的所有其他内容?我在网上看了一下,但这没有帮助。一个人提出重写整个文件,但这非常低效(还有其他+900 * 30个字符)。任何人?哦,这是我的代码:
void Unlock(int Level)
{
ifstream CheckIfExists("levels.txt");
if (!CheckIfExists.good())
{
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "ERROR",
"Unable to open the levels file. Next level won't be unlocked!", window);
CheckIfExists.close();
return;
}
CheckIfExists.close();
if(Level >= 0 && Level <= 29)
{
ofstream New_Locks("levels.txt");
Locked[Level] = UNLOCKED;
for(int i = 0; i < 30; ++i) New_Locks << Locked[i];
New_Locks.close();
return;
}
else SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "Congratulations!",
"You won! Thank you for playing! Maybe once more?", window);
return;
}
使用 fstream
而不是 ofstream
ifstream
仅用于阅读,ofstream
仅用于写作,fstream
两者兼而有之。
另请注意:没有必要显式关闭文件。流析构函数将正确关闭它们。
相关文章:
- 寻找一种更好的方法来表示无符号字符数组
- 打印字符数组地址的正确方法
- 在 std::vector<无符号字符中存储任意数据的方法>
- 在C++事务之间存储大量字符数据的有效方法
- 比较"A"、"C"、"G"'T'字符的最快方法
- 一种将 Dart 中的字节数据转换为 C++ 中的无符号字符*的有效方法?
- C++-将ASCII字符数组中可能的数值转换为字符的最简单方法
- C++ - 创建具有相同字符的特定大小的以 null 结尾的 c 样式字符串的更好方法
- c++14 - 这是在字符串上附加字符的好方法吗?
- 从ostringstream获取最长行的字符数的智能方法
- 常量字符*的模板方法专用化
- 将字符数组设置为彼此相等的方法不起作用
- 字符 - 'pub' 在 std::streambuf::p ubsetbuf() 方法中是什么意思?
- 比较字符* (C/C++) 的最快方法?
- 将大文件读取到字符数组的正确方法
- 使用 fstream 在空格分隔文件中查看下一项(不仅仅是一个字符)的方法
- 将空填充但不以空结尾的固定长度字符数组转换为 std::string 的最佳方法
- isdigit() 和 isalnum() 给出错误,因为输入是一个常量字符并且无法转换。其他可能查看输入是否为数字的方法?
- 我通过迭代加法将二进制数转换为十进制并检查单个字符(请参阅代码)的方法有什么问题?
- 如何使从基类继承的对象访问字符方法,但有自己的结果