How to check if a file is ASCII or binary in C ++

Thus, using the system batch file, we can use the file to determine if the file is ASCII Text or "data". I wanted to know if there is a way to test the code as it was? I want to basically throw a corrupted error if the file is "data". I am using ifstream to read files. Thanks for any help!

Duplicate this question .

0


source to share


1 answer


You can iterate over the bytes of a file and use std::isprint

from <cchar>

to check if that character is printable. If the file contains non-printable characters, it might be a binary file. Note that this only works for legacy encodings (like the ASCII you mentioned) and not for Unicode encoded files.



0


source







All Articles