473,387 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 5085

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Eric | last post by:
Assume that disk space is not an issue (the files will be small < 5k in general for the purpose of storing preferences) Assume that transportation to another OS may never occur. Are there...
3
by: Tron Thomas | last post by:
What does binary mode for an ofstream object do anyway? Despite which mode the stream uses, operator << writes numeric value as their ASCII representation. I read on the Internet that it is...
28
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
6
by: alice | last post by:
hi all, Can anybody please tell the advantages which the binary files offers over the character files. Thanks, Alice walls
4
by: knapak | last post by:
Hello I'm a self instructed amateur attempting to read a huge file from disk... so bear with me please... I just learned that reading a file in binary is faster than text. So I wrote the...
10
by: joelagnel | last post by:
hi friends, i've been having this confusion for about a year, i want to know the exact difference between text and binary files. using the fwrite function in c, i wrote 2 bytes of integers in...
15
by: JoeC | last post by:
I am writing a program that I am trying to learn and save binary files. This is the page I found as a source: http://www.angelfire.com/country/aldev0/cpphowto/cpp_BinaryFileIO.html I have...
3
by: masood.iqbal | last post by:
Hi, Kindly excuse my novice question. In all the literature on ifstream that I have seen, nowhere have I read what happens if you try to read a binary file using the ">>" operator. I ran into...
9
by: deepakvsoni | last post by:
are binary files portable?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.