[Qt-interest] QFile::exists on a folder

Mika Rajala mika_rajala_2 at hotmail.com
Thu Feb 11 12:01:03 CET 2010


This was my previous kludge.

 

I think the solution suggested previously by Ender EREL is a bit better

 

He adviced to use QFileInfo::isFile to check if a path points to a file.

 

Hmm, makes me wonder. If here, QFileInfo::isFile, a file is a file and never a folder,

why would QFile::exists() tell that a folder is an existing file?

 

anyway, i think it's better to use QFileInfo to check that it's a file. Although trying to open it is a valid solution as well.

 

-mika
 
> From: thiago at kde.org
> To: qt-interest at trolltech.com
> Date: Thu, 11 Feb 2010 11:47:53 +0100
> Subject: Re: [Qt-interest] QFile::exists on a folder
> 
> Em Quinta-feira 11 Fevereiro 2010, às 10:30:14, Mika Rajala escreveu:
> > Hi
> > 
> > Am I doing something wrong.
> > 
> > I want to check if a file exists prior to opening it, and I do something
> > like this:
> > 
> > void myClass::openFile(const QString & _file)
> > {
> > QFile fileToOpen(_file);
> > if(fileToOpen.exists() == false)
> > {
> > printf("Error file:\"%s\" doesn't exist",
> > _file.toStdString().c_str());
> > }
> > ....
> > }
> <
> > It all works fine, until that function is called with an absolute path to a
> > directory.
> 
> Why don't you simply try to open? I mean, why are you checking if a file exists 
> before you open it?
> 
> If it doesn't exist, it will fail to open anyway. And you can't open a 
> directory with QFile, so you also get an error there (an EISDIR).
> 
> -- 
> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
> Senior Product Manager - Nokia, Qt Development Frameworks
> PGP/GPG: 0x6EF45358; fingerprint:
> E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358
 		 	   		  
_________________________________________________________________
Uudessa IE8 selaimessa on uudet pikatoiminnot.
http://www.microsoft.com/finland/windows/products/winfamily/ie/beta/default.mspx
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100211/b6e0affd/attachment.html 


More information about the Qt-interest-old mailing list