how to use AutoCloseHandle in QFile base operation?
-
wrote on 14 Nov 2022, 12:35 last edited by Qt embedded developer
i want to know how to use AutoCloseHandle flag of Qfile in below code ?
QFile file(QDir::homePath() + "/Runtime/"+ "NewFile.txt"); if (!file.open(QIODevice::ReadWrite)) { qDebug() << "file is not exist"; qDebug()<< "Cannot open file for writing: " << qPrintable(file.errorString()); } else { QTextStream out(&file); out << secondsbetweenDateTime<< endl; }
-
i want to know how to use AutoCloseHandle flag of Qfile in below code ?
QFile file(QDir::homePath() + "/Runtime/"+ "NewFile.txt"); if (!file.open(QIODevice::ReadWrite)) { qDebug() << "file is not exist"; qDebug()<< "Cannot open file for writing: " << qPrintable(file.errorString()); } else { QTextStream out(&file); out << secondsbetweenDateTime<< endl; }
wrote on 14 Nov 2022, 13:15 last edited by@Qt-embedded-developer
Why are you interested at all in QFileDevice::AutoCloseHandle? Just for the sake of it? Since the docs state:When opening a file by name, this flag is ignored as Qt always owns the file handle and must close it.
how is it relevant to your situation?
if (!file.open(QIODevice::ReadWrite))
As I posted in your previous thread,
ReadWrite
is likely to be the wrong choice here. Up to you whether you want to respect this or ignore it. -
@Qt-embedded-developer
Why are you interested at all in QFileDevice::AutoCloseHandle? Just for the sake of it? Since the docs state:When opening a file by name, this flag is ignored as Qt always owns the file handle and must close it.
how is it relevant to your situation?
if (!file.open(QIODevice::ReadWrite))
As I posted in your previous thread,
ReadWrite
is likely to be the wrong choice here. Up to you whether you want to respect this or ignore it.wrote on 15 Nov 2022, 04:23 last edited by Qt embedded developer@JonB can you tell me what i have to write in place of below
if (!file.open(QIODevice::ReadWrite))
Because as i want to create the file when file not exist.
My file is like simple file on home directory of linux .
I asked question because i want to explore purpose of QFileDevice::AutoCloseHandle .
-
wrote on 15 Nov 2022, 04:50 last edited by Bonnie
This is only used when you open a file by an existing file handle or file descriptor.
bool QFile::open(FILE *fh, QIODevice::OpenMode mode, QFileDevice::FileHandleFlags handleFlags = DontCloseHandle)
bool QFile::open(int fd, QIODevice::OpenMode mode, QFileDevice::FileHandleFlags handleFlags = DontCloseHandle)
1/4