Check if file exists Qt
-
wrote on 7 Oct 2016, 20:56 last edited by A Former User 10 Jul 2016, 21:54
hi i tried many ways but lately take me problems? some working example. sorry again..
-
wrote on 7 Oct 2016, 21:29 last edited by
Hi!
There are several ways to do that. One is using[static] bool QFile::exists(const QString &fileName)
, e.g.:qDebug() << QFile::exists("/home/pw/docs/file.txt");
-
wrote on 8 Oct 2016, 06:59 last edited by
Hi,
You can also use QFileInfo Class to check the existance of file.
Sample code.
QString fileName("./sample.txt"); QFile file(fileName); if(QFileInfo::exists(fileName)) { qDebug () << "file exists" << endl; file.open(QIODevice::ReadWrite | QIODevice::Text); QString data = file.readAll(); qDebug () << "data in file:" << data << endl; qDebug()<<"file already created"<<endl; file.close(); } else { qDebug () << "file does not exists" << endl; file.open(QIODevice::ReadWrite | QIODevice::Text); file.write("Qt Programming"); qDebug()<<"file created"<<endl; file.close(); }
Checks files exists, if not exists, file gets created and data is written to the file.
If file exists read the data from the file.Thanks,
-
Hi!
There are several ways to do that. One is using[static] bool QFile::exists(const QString &fileName)
, e.g.:qDebug() << QFile::exists("/home/pw/docs/file.txt");
wrote on 8 Oct 2016, 07:05 last edited byAs @Wieland said many ways to check
There are several ways to do that. One is using
[static] bool QFile::exists(const QString &fileName)
, e.g.:qDebug() << QFile::exists("/home/pw/docs/file.txt");
QString fileName("./sample.txt"); QFile file(fileName); qDebug () << file.exists();
exists() method with parameter and without parameter.
Thanks,
-
wrote on 8 Oct 2016, 07:07 last edited by
Another way:
qDebug() << QDir("/home/pw/docs").exists("file.txt");
-
Another way:
qDebug() << QDir("/home/pw/docs").exists("file.txt");
wrote on 8 Oct 2016, 07:08 last edited by@Wieland said in Check if file exists Qt:
Another way:
qDebug() << QDir("/home/pw/docs").exists("file.txt");
Nice Info.
2/6