Detecting binary files

D

dagecko

Hi

I would like to know how to detect if a file is binary or not.
It's important for me but I don't know where to start.

Ty
 
R

roberts.noah

You can't.

More to the point, there is no difference. All files are "binary".
The difference between opening them in binary vs. text *mode* is that
text mode does some translations during IO operations and binary does
not. This happens in the software during runtime, it isn't a state
that is saved in the file or that can be tested.
 
V

Victor Bazarov

dagecko said:
I would like to know how to detect if a file is binary or not.
It's important for me but I don't know where to start.

One criterion I know of is that "non-binary" files should not contain
any non-printable characters. So, open the file and read it all in,
trying to find non-printable characters. As soon as you've found one,
deem the file binary.

Of course, your definition of "binary" can be different, so, as soon as
you find what it means to you, we can help you figure out how to put it
in C++ terms.

V
 
D

dagecko

Le Wed, 08 Feb 2006 09:15:53 -0800, roberts.noah a écrit :
More to the point, there is no difference. All files are "binary".
The difference between opening them in binary vs. text *mode* is that
text mode does some translations during IO operations and binary does
not. This happens in the software during runtime, it isn't a state
that is saved in the file or that can be tested.

The main concern is that I tried to make a program that can
replaces strings with other strings. This works well for ascii
files, but with binary files (and even if I read/write with
ios_base::binary), the result for binary files is corrupted:
the size of the newly created binary file is smaller and
could not be executed (even for simple strings with same
sizes).

I might not have used the best stuff to do that (I'm not an
expert in file programming :): I often use \0. I guess this
can become from that point.

Although I first wasn't interrested in replacing strings
from binary files I guess it could still be a good point if
I succeed to do so (as some figure that it's not possible
to detect if a file is a binary or not).
 
T

TB

dagecko sade:
Le Wed, 08 Feb 2006 09:15:53 -0800, roberts.noah a écrit :


The main concern is that I tried to make a program that can
replaces strings with other strings. This works well for ascii
files, but with binary files (and even if I read/write with
ios_base::binary), the result for binary files is corrupted:
the size of the newly created binary file is smaller and
could not be executed (even for simple strings with same
sizes).

Are you trying to trash executable files? COFF-images carry
checksums, and I'd guess ELF-images (and most others) do
the same. And note that "binary" does not imply "executable".
 
J

Jay_Nabonne

Le Wed, 08 Feb 2006 09:15:53 -0800, roberts.noah a écrit :


The main concern is that I tried to make a program that can
replaces strings with other strings. This works well for ascii
files, but with binary files (and even if I read/write with
ios_base::binary), the result for binary files is corrupted:
the size of the newly created binary file is smaller and
could not be executed (even for simple strings with same
sizes).

I think the best place to detect a binary file is at the "user" level.
Your program will do what it does, as directed by the user. If you come up
with code that works with binary files, the user can supply a binary
switch to use it in that mode. If the user shoots himself in the foot,
well that's the problem of the tool. (We use hammers to pound nails and
screwdrivers to drive screws. If someone wants to pound a screw with a
hammer, that's their choice. It may or may not give the result they want,
but then again it may. The user of the tool can make the best assessment
of applicability of a tool.)

Just my opinion, of course...

- Jay
 
D

dagecko

Le Wed, 08 Feb 2006 19:11:11 +0100, TB a écrit :
dagecko sade:

Are you trying to trash executable files? COFF-images carry
checksums, and I'd guess ELF-images (and most others) do
the same. And note that "binary" does not imply "executable".

I know binary does not imply executable. I also have several
binary files containing plenty of data.

Well, the main problem of 'self-shooting users' is a good
point, but isn't so good in my program. I try to explain:
I made an option which will change strings of all files in
a directory, and another option will tell the program to do
it recursively (so all files in subdirectories and so on).

Well anyway, thank you all, I'll try to find a good solution.
 

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,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top