Visit Our UNIX and Linux User Community


writing binary file (C++)


 
Thread Tools Search this Thread
Top Forums Programming writing binary file (C++)
# 1  
Old 06-10-2011
writing binary file (C++)

Hi guys, I am writing a vector into a binary file (linux of course), but somehow there seems to be something wrong with this code, because the output file size in binary is BIGGER than doing it with the same vector but insead of binary, in plain text, and I as far as I understand, binary files size it's lesser than saving it as a text.

Here's the code

Code:
	
int aux=0;
	    ofstream fo(file, ios::out | ios::binary);
	    fo.write("MP-PER-B \n",10*sizeof(char));
	    fo.write((const char*) (&numero),sizeof(int));
	    for(int i=0; i<numero; i++){
			aux=vector[i];
			fo.write ((const char *) (&aux),sizeof(int));
		}	
	  fo.close();

Also, being unable to read it from linux command line makes things difficult (I can't see if it wrote the numbers since I do not know any command that would read the file made ).
# 2  
Old 06-10-2011
I like cat -vt and od -bc for looking at binary files, to see what is extra. If you are hex or decimal not octal, adjust od options per man od.
# 3  
Old 06-10-2011
Quote:
Originally Posted by lamachejo
and I as far as I understand, binary files size it's lesser than saving it as a text.
Depends what's in it. Your integers are probably four bytes. "32\n" would be slightly smaller. Whereas binary is always the same size every time.
Quote:
Also, being unable to read it from linux command line makes things difficult (I can't see if it wrote the numbers since I do not know any command that would read the file made ).
hexdump -C filename
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 06-10-2011
Quote:
Originally Posted by Corona688
Depends what's in it. Your integers are probably four bytes. "32\n" would be slightly smaller. Whereas binary is always the same size every time. hexdump -C filename
Doing the hexdump -C shows letters and symbols, (and the MP-PER-B at the first line)...

It's not supposed to do that, right? It should print numbers since I wrote (or at least that was my intention Smilie ) numbers in the file.
# 5  
Old 06-10-2011
Yes, but realize that in x86 and other systems, integers and floats are little-endian, so a short of 258 is hex 0201, whereas in SPARC and other big-endian systems, and in Internet packet headers, it is hex 0102.

Second, "#pragma pack" tells the compiler what modulus to allocate storage on, so if you write a struct or such, it may be padded.
# 6  
Old 06-10-2011
Is it any different on 64 bits OS?
# 7  
Old 06-10-2011
Quote:
Originally Posted by lamachejo
Doing the hexdump -C shows letters and symbols, (and the MP-PER-B at the first line)...
Look closer.

Code:
#include <stdio.h>
int main(void)
{
        int n=32;
        fwrite(&n, 1, sizeof(n), stdout);
}

Code:
$ ./a.out > bin.bin
$ hexdump -C bin.bin
00000000  20 00 00 00                                       | ...|
00000004

Imagine that; you get four binary bytes representing the integer 32. Because this is a little-endian system, they end up all backwards. Try echo $((0x00000020)) in your shell.
Quote:
It's not supposed to do that, right?
It is.
Quote:
It should print numbers since I wrote (or at least that was my intention Smilie ) numbers in the file.
They are numbers. Binary numbers.

---------- Post updated at 09:14 AM ---------- Previous update was at 09:08 AM ----------

Quote:
Originally Posted by lamachejo
Is it any different on 64 bits OS?
'int' types are 32-bit even on most 64-bit systems. long integers, though, are generally 64-bit on 64-bit systems (and 32-bit elsewhere). And if your system is neither 32 nor 64 bits, all bets are off.

If you're concerned about your integers changing size when your code gets moved, you can #include <stdint.h> and use int32_t to get a 32-bit integer that'll always be a 32-bit integer.
This User Gave Thanks to Corona688 For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #377
Difficulty: Medium
macOS is UNIX V7 compliant.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script writing for tables with binary data

Hi There, I'm trying to write a simple script that will email me when we have an application job in a certain status that needs human intervention. I've used this script for other tables and it works great. However, this one gives me the warning that there is binary data so it might not. ... (2 Replies)
Discussion started by: Colel2
2 Replies

2. Programming

Writing a file in Binary

Hello All, I need to read values from database and then need to write to a file. Planning to do that as binary since no one can modify the values. We tried to write the file in "B" mode and the file is created but when I try to open in notepad I am able to do. I there a way that someone should... (4 Replies)
Discussion started by: arunkumar_mca
4 Replies

3. Shell Programming and Scripting

Convert binary file to csv and then back to the binary format

Hello *nix specialists, Im working for a non profit organisation in Germany to transport DSL over WLAN to people in areas without no DSL. We are using Linksys WRT 54 router with DD-WRT firmware There are at the moment over 180 router running but we have to change some settings next time. So my... (7 Replies)
Discussion started by: digidax
7 Replies

4. UNIX for Dummies Questions & Answers

[AIX] Binary file warning for text file.

Hello guys, We had to move from a DC to another, and we are now facing an "issue" with some text files. Looks like that some of our log files are set as binary: file TuxConnectorURA.20121012 TuxConnectorURA.20121012: data or International Language text less TuxConnectorURA.20121012... (2 Replies)
Discussion started by: EnioMarques
2 Replies

5. Shell Programming and Scripting

Output redirection of c binary file to a file in shell script is failing

I am struck up with a problem and that is with output redirection. I used all the ways for the redirection of the output of c binary to a file, still it is failing. Here are the different ways which I have used: ./a.out | tee -a /root/tmp.txt 2>&1 ./a.out | tee -a /root/tmp.txt 1>&1 ./a.out |... (2 Replies)
Discussion started by: Maya29988
2 Replies

6. UNIX for Advanced & Expert Users

How to copy a binary file while the file is being written to by another process

Hello, Can I copy a binary file while the file is being written to by another process? Another process (program) “P1” creates and opens (for writing) binary file “ABC” on local disk. Process P1 continuously write into ABC file every couple of seconds, adding 512-byte blocks of data. ABC file... (1 Reply)
Discussion started by: mbuki
1 Replies

7. UNIX for Dummies Questions & Answers

Pipe binary file matches grep results to file

I am using grep to match a pattern, but the output is strange. $ grep -r -o "pattern" * Gives me: Binary file foo1 matches Binary file foo2 matches Binary file foo3 matches To find the lines before/after, I then have to use the following on each file: $ strings foo1 | grep -A1 -B1... (0 Replies)
Discussion started by: chipperuga
0 Replies

8. Shell Programming and Scripting

To log binary file output to a txt file

Hi, I wrote a small script whose function is to execute the postemsg provided if the threshold breaches. I want to log this postemsg messages to a log file. But I am not able to do. Can someone throw some light on how to log the output of this. I am pasting a snippet of that code. ... (2 Replies)
Discussion started by: dbashyam
2 Replies

9. Programming

writing binary/struct data to file

I am trying to write binary data to a file. My program below: #include <stdlib.h> #include <stdio.h> struct tinner { int j; int k; }; struct touter { int i; struct tinner *inner; }; int main() { struct touter data; data.i = 10; struct tinner... (4 Replies)
Discussion started by: radiatejava
4 Replies

10. Solaris

compiled binary file gives "cannot execute binary file"

Hi, I have two Solaris machines. 1. SunOS X 5.8 Generic_108528-29 sun4u sparc SUNW,Sun-Blade-1500 2. SunOS Y 5.8 Generic_108528-13 sun4u sparc SUNW,Ultra-60 I am trying to buiild a project on both these machines. The Binary output file compiled on machine 2 runs on both the machines. Where... (0 Replies)
Discussion started by: scgupta
0 Replies

Featured Tech Videos