473,769 Members | 6,305 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5125

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...@gm ail.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...@gm ail.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::binar y), 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...@gm ail.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::binar y), 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::binar y), 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...@gm ail.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::binar y), 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
5039
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 any solid reasons to prefer text files over binary files files?
3
3496
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 possible to change the behavior of operator << so it will stream numeric values as their actual values when an ofstream is in binary mode. I did not, however, find any information on how this can be accomplished. What is involved in getting this...
28
2802
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
6
2255
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
3695
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 following code that compiles OK. It runs and shows the requested output. However, after execution, it pops one of those windows to send error reports online to the porgram creator. I have managed to find where the error is but can't see what's wrong....
10
3664
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 binary mode. according to me, notepad opens files and each byte of the file read, it converts that byte from ascii to its correct character and displays
15
3004
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 successfully created and used txt files. I am trying to save then load in an array of pointers to objects:
3
3849
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 the two problems while trying to read a binary file. 1). All whitespace characters were skipped 2). Certain binary files gave a core dump
9
2893
by: deepakvsoni | last post by:
are binary files portable?
1
9993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.