How to know if a file is a text file or a binary file?
I want to search a string in all text files in a directory. Encoding could be either ASCII or UTF8. there is no convention in naming. for example a c++ source file could be named like 'a.jpg'. before trying the 'file' command of unix-like systems I want to know if there is a Qt way for this? or maybe cross-platform?...
AFAIK It not possible on Qt
but you can try use libmagic for check MIME type of file.
See my blog post "here":http://va-sorokin.blogspot.com/2011/03/how-to-get-mime-type-on-nix-system.html
"Here":http://gnuwin32.sourceforge.net/packages/file.htm windows version of this libary
But we can determine if a file is executable or not using "QFileInfo::isExecutable ()":http://doc.qt.nokia.com/latest/qfileinfo.html#isExecutable
[quote author="Rahul Das" date="1317143517"]But we can determine if a file is executable or not using "QFileInfo::isExecutable ()":http://doc.qt.nokia.com/latest/qfileinfo.html#isExecutable[/quote]
Some file formats are binary and they are not executable.
THere is no generic way to check, if a file is binary or text.
If you create a binary file, you can set some specific first bytes to check a file type, but I know no generic way for that.
What you could do is start looking for the text, and stop when you either find that text, or find something that is not text, and do that for all files. Exactly what you want to consider as not text depends on possible text encodings of course, but if you are limited to ASCII and UTF8, it might not be too difficult.
This topic similar with your first question: http://developer.qt.nokia.com/forums/viewthread/10140/
[Merged: from similar topic]
[quote author="Vass" date="1318355648"]This topic similar with your first question: http://developer.qt.nokia.com/forums/viewthread/10140/
Please, don't mixed different question in one topic.
You can rename/edit this topic for your second question and discuss you first question in topic, link for which I printed above.[/quote]
That was my question! Not feedmeqt's :-/
There is no way to reliably test file types in a general way.
@soroush Yes, you are right it was your question, but about same thing.
sorry soroush !!! since my keywords used for searching this particular topic didn't find me any solution and I didn't done much research on it. Since QT guys always repsonded me quickly!!! :)
yes, I thought of making it as separate question. But, got more interested in finding solution(New to C++ and QT) .!!