繁体   English   中英

无法写入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文件,该函数运行时没有任何错误,但文件保持不变。 我不明白为什么会这样:(。感谢halp :)

您不能以这种方式修改文件。 最简单的方法是将文件读入为字符串向量(每行一个字符串),修改内存中的行,然后再次保存行。

暂无
暂无

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

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