471,595 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,595 software developers and data experts.

Detecting binary files

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
Feb 8 '06 #1
8 4875

dagecko wrote:
Hi

I would like to know how to detect if a file is binary or not.


You can't.

Feb 8 '06 #2

roberts.n...@gmail.com wrote:
dagecko wrote:
Hi

I would like to know how to detect if a file is binary or not.


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.

Feb 8 '06 #3
dagecko wrote:
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
--
Please remove capital As from my address when replying by mail
Feb 8 '06 #4
Le Wed, 08 Feb 2006 09:15:53 -0800, roberts.noah a écrit*:

roberts.n...@gmail.com wrote:
dagecko wrote:
> Hi
>
> I would like to know how to detect if a file is binary or not.


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.


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).

Feb 8 '06 #5
TB
dagecko sade:
Le Wed, 08 Feb 2006 09:15:53 -0800, roberts.noah a écrit :
roberts.n...@gmail.com wrote:
dagecko wrote:
Hi

I would like to know how to detect if a file is binary or not.
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.


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".

--
TB @ SWEDEN
Feb 8 '06 #6
On Wed, 08 Feb 2006 18:53:12 +0100, dagecko wrote:
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
Feb 8 '06 #7
On Wed, 08 Feb 2006 18:15:19 +0000, Jay_Nabonne wrote:

Correction:
If the user shoots himself in the foot, well that's *NOT* the problem of
the tool.

Feb 8 '06 #8
Le Wed, 08 Feb 2006 19:11:11 +0100, TB a écrit*:
dagecko sade:
Le Wed, 08 Feb 2006 09:15:53 -0800, roberts.noah a écrit :
roberts.n...@gmail.com wrote:
dagecko wrote:
> Hi
>
> I would like to know how to detect if a file is binary or not.
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.


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".


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.
Feb 8 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

27 posts views Thread by Eric | last post: by
3 posts views Thread by Tron Thomas | last post: by
6 posts views Thread by alice | last post: by
4 posts views Thread by knapak | last post: by
10 posts views Thread by joelagnel | last post: by
15 posts views Thread by JoeC | last post: by
3 posts views Thread by masood.iqbal | last post: by
9 posts views Thread by deepakvsoni | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.