Pregunta

is there any "good" way to distinguish between movie file and image file? I would like to know what exactly my "std::wstring filePath" is - a movie, or an image. Therefore, I could go further with strong assurance I am working with known file type.

In other words, I have two classes MyImage and MyMovie both need path to file in their constructors. I would like to verify path to file somehow before creating one of those classes.

bool isMovie(const std::wstring & filePath);
bool isImage(const std::wstring & filePath);

Of course I thought about file extensions, but I'm not sure that it is good and not prone to errors solution. So is it good to use file extension or any other feasible solution is possible.?

Thanks in advance

¿Fue útil?

Solución

You can use libmagic to detect what kind of file it is. You pass the file path in and it'll give you a textual description or MIME type for the file.

Otros consejos

Usually files have special so called magic bytes. I you have a control over the specification I would use this. If you try opening zip, gif, or other binary stuff you can usually find some distinctive strings there.

There is a unix tool utility called file that provides such functionality, so probably some sort of standard exists.

SQLite 3 provides a nice example. Look at 1.2.1 and 1.2.5. So not only the info that it is a SQLite 3 DB is given, but also additional application id, so other tools can recognize which application's DB it is.

I personally used few first bytes of a file to code type and version info for my files when I was playing with binary stuff.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top