views:

1807

answers:

2

So using the system command file we can use file to determine if a file is ASCII Text or "data". I wanted to know if there is a way to check in code which one it was? I want to basically throw a corrupt error if the file is 'data'. I am using ifstream for reading the files. Thanks for any help!

Duplicate of this question.

+3  A: 

This question has already been answered on StackOverflow here

Johannes Schaub - litb
I thought exactly the same. :-D
Tomalak
A: 

You can iterate over the bytes of the file and use std::isprint from <cchar> to test whether the character is printable. If there are nonprintable characters in the file, chances are it's a binary file. Notice that this only works for legacy encodings (e.g. ASCII mentioned by you), not for Unicode-encoded files.

Konrad Rudolph