473,405 Members | 2,141 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,405 software developers and data experts.

Binary file output.

1
Here is what I'm doing:

Expand|Select|Wrap|Line Numbers
  1. file.write((char *) obj_ref, sizeof *obj_ref);
The output looks like a text file with no formating. Aren't binary files supposed to contain binary numbers? lol. What am I missing?

blanny
Dec 16 '06 #1
2 2210
Expand|Select|Wrap|Line Numbers
  1. file.open("filename",ios::ate|ios::in|ios::binary|ios::out);
Here is what I'm doing:

Expand|Select|Wrap|Line Numbers
  1. file.write((char *) obj_ref, sizeof *obj_ref);
The output looks like a text file with no formating. Aren't binary files supposed to contain binary numbers? lol. What am I missing?

blanny
when you open file then you include ios::binary in your statement
like
file.open("file name",ios::in| ios::out|ios::ate|ios::binary);
Dec 19 '06 #2
willakawill
1,646 1GB
Here is what I'm doing:

Expand|Select|Wrap|Line Numbers
  1. file.write((char *) obj_ref, sizeof *obj_ref);
The output looks like a text file with no formating. Aren't binary files supposed to contain binary numbers? lol. What am I missing?

blanny
Hi. Not sure what your question is.
When you write a binary file it is one char at a time, i.e. one byte at a time.
this could be one character byte, one part of the 4 bytes of a float etc. It does not 'look' like anything. When you open it to see what it 'looks' like, the appearance will depend on what app you are using for this purpose so the 8 bit bytes will be interpreted by the app. individually and will normally make no sense if there is numeric data included.

When writing and reading binary format you must cast the variable to char * so that each byte will be written or read separately. In the case of an array for example, you will need the number of elements in the array plus the size of each array element. e.g. an array of long:

Expand|Select|Wrap|Line Numbers
  1. ifst.read(reinterpret_cast<char *> (myarray), arrsize * 4);
If the array was of type char then:

Expand|Select|Wrap|Line Numbers
  1. ifst.read(myarray, arrsize);
Dec 19 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Idar | last post by:
Is there an effecient/fast way in python to convert binary data from file (24bit hex(int) big endian) to 32bit int (little endian)? Have seen struct.unpack, but I am unsure how and what Python has...
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...
103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
8
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
28
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
2
by: anirudhvr | last post by:
Hi, I needed a basic binary to ascii encoder, so I wrote this piece of code: /* Encoding algo: suppose 11111010 is the byte to be encoded. It is first broken up into two 4-bit parts (1111...
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
8
by: Mark | last post by:
Hello. I am attempting to write binary data from a file to an OLE Object field, and then write the file back out from the database. I am reading and writing the files in binary mode, and using...
1
by: yohan610 | last post by:
i have to read the binary data of a file, then encrypt them according to a supplied algorithm...and then the obtained output has to be written to an output file...everything works ok, and there are...
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.