Ask the Expert

How do I check whether a file is a text file or binary file using C/C++?

How do I check whether a file is a text file or binary file using C/C++?
Probably the easiest way is to use the C Run Time function, isprint. That will determine if a byte is a printable character. As you scoot through the file, as soon as you find a byte that's not printable, you can probably safely assume it's not a text file. Of course, if you want to allow UNICODE text files, you'll have to do much more work, which is not fool proof.

This was first published in September 2002

Join the conversationComment

Share
Comments

    Results

    Contribute to the conversation

    All fields are required. Comments will appear at the bottom of the article.