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++?

    Requires Free Membership to View

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: