0
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;
        }
    }
}

I'm trying to write to the fstram file, the function run without any errors, but the file stay the same. I can't understand why it's happen :(. Thanks for the halp :)

ronbob
  • 13
  • 5

1 Answers1

0

You can't modify a file in place in that way. It would be easiest to read the file in as a vector of strings (a string for each line), modify the line in memory, and then save the lines out again.

James Hopkin
  • 13,099
  • 1
  • 38
  • 68