0

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.

Community
  • 1
  • 1

1 Answers1

0

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
  • 530,221
  • 131
  • 937
  • 1,214