How to know if a file is a text file or a binary file?
-
wrote on 27 Sept 2011, 15:17 last edited by
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?...
-
wrote on 27 Sept 2011, 16:04 last edited by
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
-
wrote on 27 Sept 2011, 17:11 last edited by
But we can determine if a file is executable or not using "QFileInfo::isExecutable ()":http://doc.qt.nokia.com/latest/qfileinfo.html#isExecutable
-
wrote on 27 Sept 2011, 18:36 last edited by
[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.
-
wrote on 27 Sept 2011, 18:49 last edited by
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. -
wrote on 28 Sept 2011, 11:45 last edited by
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.
-
wrote on 11 Oct 2011, 17:54 last edited by
This topic similar with your first question: http://developer.qt.nokia.com/forums/viewthread/10140/
[Merged: from similar topic]
-
wrote on 11 Oct 2011, 18:09 last edited by
[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 :-/
-
wrote on 11 Oct 2011, 18:20 last edited by
There is no way to reliably test file types in a general way.
-
wrote on 12 Oct 2011, 03:30 last edited by
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) .!!