Linux C++ Qt fixing "QIODevice::write: ReadOnly device"?

So the problem is, I am trying to write to a file with QFile but i am getting the error:

QIODevice::write: ReadOnly device

My implementation looks like:

void logList::insert(QString data) {
    QString lin;
    QFile file1("log.data");
    file1.open(QIODevice::WriteOnly | QIODevice::Text);
    QTextStream out("log.data");
    lin = out.readLine();
    out << data;
    file1.seek(30);
    file1.close();
}

I have also tried with

QIODevice::ReadWrite 

for the open()

and

system("chmod 777 log.data");

So how do I read and write from a file in linux?

Thanks

Upvotes: 1

Views: 3123

Answers (1)

TheDarkKnight
TheDarkKnight

Reputation: 27611

QTextStream out("log.data");

The QTextStream constructor that takes a string does not open the file, or work on a file opened with that name.

Instead, you should pass the QFile object to QTextStream

QTextStream out(&file1);

In addition, if you want to read from the file, this won't work

file1.open(QIODevice::WriteOnly | QIODevice::Text);

You need to use the QIODevice::ReadWrite flag, instead of QIODevice::WriteOnly

Finally, if you're using QTextStream instead of QFile's read / write functions, it would be better to seek with the QTextStream, rather than the QFile object

out.seek(30);

Upvotes: 2

Related Questions