Discerning between ASCII and binary STL files

My program accepts STL (the 3d printing type) files, and currently works with the ASCII type.
I would like to create a process that identifies the file as either ASCII or binary.
I am thinking that the header is enough to tell the difference, as an ASCII header begins with the word "solid" while binary files never do.
How would I extract the first word of the file and interpret it as either "solid" or something else?
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.

http://www.cplusplus.com/reference/cctype/isprint/

you might also look at isascii.
> How would I extract the first word of the file and interpret it as either "solid" or something else?


1
2
3
4
5
6
std::string first_token_in( std::string file_name )
{
    std::string token ;
    std::ifstream(file_name) >> token ;
    return token ;
}

http://coliru.stacked-crooked.com/a/884573c1cbafe7fb
http://rextester.com/BSWV98257
On Unix-like systems, libmagic offers a way to do this.
It's how file (1) (attempts to) work:
https://linux.die.net/man/1/file
Last edited on
Topic archived. No new replies allowed.