Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwrite text file vs append

Tags:

qt

qfile

I'm looking to overwrite data in a text file but all I can seem to do is append to it

mFile.open(QFile::ReadWrite)

QTextStream in(&mFile);
QString first = in.readLine(); //discard the headers
QString dataLine = in.readLine(); //headers
QStringList sql_row = dataLine.split("\t"); //first row (sake of proj only 1 row)

if(sql_row[1].isEmpty()) //no user name registered
{
    QByteArray user= getenv("USERNAME"); //for windows
    if(user.isEmpty())
        {
                user = getenv("USER"); ///for MAc or Linux
            }
    dataLine = dataLine.insert(dataLine.indexOf("\t")+ 1,user);

    in << first << endl << dataLine << endl;
    mFile.flush();
    mFile.close();
like image 972
Matt Stokes Avatar asked Nov 09 '12 19:11

Matt Stokes


People also ask

What is the difference between append and overwrite?

The append option, which is the default option, adds the copied data to the existing data for the target members. The overwrite option takes the copied data and replaces the existing data for the target members.

Does file write append or overwrite?

The write method overwrites the content in a text file while the append method appends text to the file.

What is the difference between writing a file and appending a file?

The difference between appending and writing is that when you write to a file, you erase whatever was previously there whereas when you append to a file, you simply add the new information to the end of whatever text was already there.

What does it mean to overwrite a file?

If you overwrite a computer file, you replace it with a different one.


2 Answers

Change

mFile.open(QFile::ReadWrite);

to

mFile.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text);

The QIODevice vs QFile distinction isn't necessary, but I personally favor using the base class. The Truncate flag will overwrite (i.e., delete) an existing file.

Alternatively, you can follow the other suggestion and open your text file directly using one of QTextStream's constructors. The same QIODevice::OpenMode conventions apply. This only works if mFile is a FILE object and not a QFile, which isn't the case in your example.


A couple additional notes for beginners.

Related Note 1

You didn't ask about this, but I also added the QIODevice::Text flag to ensure that newline characters get translated to/from the local encoding (plain \n vs. \r\n) when you use endl.

A really common mistake is to use \r\n AND QIODevice::Text, which results in text files with double-returns \r\r\n on Windows. Just use QIODevice::Text when opening and simply \n or endl and you'll never have this problem.

Related Note 2

Using QTextStream::endl will automatically call flush() each time. If your loop is large, use "\n" instead to prevent a slowdown unless you actually need to flush every line. The stream will automatically write to disk as its buffer gets full, or when it's closed.

QFile::close() also calls flush(), which makes your mFile.flush() at the end redundant.

like image 126
Phlucious Avatar answered Sep 20 '22 08:09

Phlucious


Use an overloaded constructor of QTextStream:

QTextStream in(&mFile, QIODevice::ReadWrite | QIODevice::Truncate);

The QIODevice::Truncate will remove all the previous content of the file, and QIODevice::ReadWrite will open it for read and write access.

like image 28
Vinícius Gobbo A. de Oliveira Avatar answered Sep 18 '22 08:09

Vinícius Gobbo A. de Oliveira