I need to write C code that checks to see if a file is text(ASCII) or Binary
Could someone help? Thanks
You can use file --mime-encoding | grep binary to detect if a file is a binary file.
A text file is the one in which data is stored in the form of ASCII characters and is normally used for storing a stream of characters.
If a file contains only the decimal bytes 9–13, 32–126, it's probably a pure ASCII text file. Otherwise, it's not. However, it may still be text in another encoding.
In text file, text, character, numbers are stored one character per byte i.e. 32667 occupies 5 bytes even though it occupies 2 bytes in memory. In binary file data is stored in binary format and each data would occupy the same number of bytes on disks as it occupies in memory.
Typical method is to read the first several hundred bytes and look for ASCII NUL.
If the file contains NUL, it is definitely a binary file. Most binary files do contain NUL bytes, but text files should never contain NUL bytes.
#include <string.h>
bool is_binary(const void *data, size_t len)
{
return memchr(data, '\0', len) != NULL;
}
Be warned that this is a heuristic. In other words, it will be wrong sometimes.
Read all characters and see if all of them are ASCII, that is, with codes from 0 to 127 inclusive.
Some tools determine whether a file is a text file or a binary file by just checking whether or not it has any byte with code 0.
Clearly, if you apply both of these methods, you will get different results for some files, so, you have to define what it is exactly that you're looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With