PDA

View Full Version : How to know file type



nrabara
16th December 2009, 10:46
Hi,

How to know what's the file type in qt. I am using following method to know the file type.


QString fileName("/home/nirav/oneStar.jpg");

if(fileName.endsWith(".jpg",Qt::CaseSensitive);
qDebug()<< "File type is jpg" ;
else
qDebug()<< "File unknow" ;



But if image file only contains name(i.e oneStar), how to know that file is of Image type.
I have checked QDir,QFile,QFileinfo but no one provides file type, it can only differentiate File or Directory or System link.

can anybody suggest me the right way to find the file type.

You suggestion would be great help for me.

calhal
16th December 2009, 13:08
Do not expect Qt to know every possible file type.
If it's some kind of bitmap you can use QImage or QPixmap to load the file without specifying the format - in that case the loader will try to guess the format by reading the file header and return appropriate value if there's a problem to determine it.

Edit:
Forgot to mention that after opening the file you can use for example
QImage::format().

Edit2:
Actually maybe QImageReader::format() would be better ;)

squidge
16th December 2009, 13:30
Linux attempts to know as many file types as possible, you can use the Linux MIME system to detect the type. On Windows you are stuck with the file extension unless you open the files yourself and detect from a signature.