无法写入 fstream 文件
cant write to fstream file
void readersWriters::WriteLine(int lineNumber, std::string newLine)
{
fstream file(_fileName, std::fstream::out | std::fstream::in);
if (file.is_open())
{
int count = 1;
string line_data;
bool found = false;
while (!file.eof() && !found)
{
getline(file, line_data);
if (count == lineNumber)
{
writeLock();
found = true;
if (line_data.length() > newLine.length())
newLine += line_data.substr(newLine.length(), line_data.length());
file << newLine;
getline(file, line_data);
writeUnlock();
}
count++;
}
file.close();
if (!found)
{
cout << ERROR_WRITE << endl;
}
}
}
我正在尝试写入 fstram 文件,该函数运行没有任何错误,但文件保持不变。我不明白为什么会:(发生。感谢您的减半:)
您不能以这种方式就地修改文件。最简单的方法是将文件作为字符串向量(每行一个字符串)读取,修改内存中的行,然后再次保存这些行。
相关文章:
- 使用fstream对txt文件中的数字进行循环
- 为什么 fstream 在打开带有格式的文件时会导致分段错误?
- 无法打开 fstream C++文件,即使它与 .cpp 位于同一位置
- 使 std::fstream 写入文件末尾,但从头开始读取
- 如何修复函数中的 fstream 文件输入以将正确的信息存储在结构数组中?
- 如何检查文件是否已存在于 fstream C++中
- 使用 fstream 库并在屏幕上打印的文件有问题?
- Windows std::fstream 修改文件后打开()?
- 如何删除文件中的数字,通过 c++ fstream 的
- 如何使用单个fstream创建、读取和写入文件
- 变量 std::fstream 文件具有初始值设定项,但类型不完整 编译错误
- STD :: FSTREAM文件处理重复使用的问题
- 如何通过C++将 fstream 文件放入头文件中
- 您可以使用指针指向FSTREAM文件吗?
- 可移植fstream文件路径的正确语法是什么
- 保存系统未打开fstream文件
- 无法写入 fstream 文件
- 是否有可能通过使用openMP在fstream(文件读取)上获得一些性能提升
- Fstream文件正在被重写
- 根据ID/Registration搜索和编辑.txt fstream文件