Preventing binary input

A

Avinash

I am writing an application that takes a file as an input.
I want to avoid binary files that have been specified by the user.
Is there any way to detect that a file contains binary data?

Thanks,
Avinash.
 
V

Victor Bazarov

Avinash said:
I am writing an application that takes a file as an input.
I want to avoid binary files that have been specified by the user.
Is there any way to detect that a file contains binary data?

No. The distinction between "binary" and "text" is purely
speculative and exists only in our heads. The same data
repository [on any external storage] can be opened as binary
or text depending on our intentions.

V
 
O

Obnoxious User

Avinash skrev:
I am writing an application that takes a file as an input.
I want to avoid binary files that have been specified by the user.
Is there any way to detect that a file contains binary data?

You could parse while you're not encountering illegal codepoints,
and terminate if you do. But what constitutes as 'illegal codepoints'
is up to you. Preparse it to validate.
 
J

James Kanze

I am writing an application that takes a file as an input.
I want to avoid binary files that have been specified by the user.
Is there any way to detect that a file contains binary data?

It depends on the system. On some systems, you can't open a
binary file in text mode, so the open would fail. This isn't
the case for Unix or Windows, however. Other than that, you can
look at the first n bytes (for some appropriate n), and use a
heuristic to guess: historically, the presence of nul bytes, for
example, or for that matter, on most systems, the presence of
any bytes in the ranges 0x00-0x06 or 0x0E-0x1F generally means
binary; so may (often) a byte in the range 0x80-0x9F. This
depend on the text encoding used, however: the sequence 0xC3,
0x89 is a capital E with an acute accent in UTF-8, and nul bytes
are likely to be pretty frequent if UTF-16 or UTF-32 is used.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top