简体   繁体   中英

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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