Binary files help

closed account (ivDwAqkS)
this is a binary file program, but how can I read it because when I create it and input the filename, age, name , etc, it outputs on the txt file something like this:

name hA Ðþ(
why? I think it should be wrote in binary number or hexadecimal numbers right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cstring>
using namespace std;

int get_int(int defaul_value);

int main(){
    char filename[MAX_PATH + 1];
    int n = 0;
    char name[20];
    int age = 0;
    int recsize = sizeof(name) + sizeof(int);

    cout << "Enter filename: ";
    cin.getline(filename, MAX_PATH);

    fstream fbin(filename, ios::binary | ios::out);
    if(!fbin){
        cout << filename << " Could not be opened";
        return -1;
    }

    cout << "Enter file record number: ";
    n = get_int(0);

    cout << "Enter name: ";
    cin.getline(name, sizeof(name)-1);
    cout << "Enter age: ";
    age = get_int(0);

    fbin.seekp(n * recsize);
    fbin.write(name, sizeof(name)-1);
    fbin.write((char*)(&age), sizeof(int));
    fbin.close();

    return 0;
}

#define COL_WIDTH 80

int get_int(int default_value){
    char s[COL_WIDTH + 1];
    cin.getline(s, COL_WIDTH);
    if(strlen(s)==0)
        return default_value;
    return atoi(s);
}
Last edited on
binary file
it outputs on the txt file


Which is it? ;P binary or text?


Anyway... to really understand what binary files are and how they work, you should probably get a hex editor. I recommend HxD, as it's probably the best free Hex Editor I've ever used.

It's available here:
http://mh-nexus.de/en/hxd/


I ran your program on my machine... input
filename = out.bin
record number = 0
name = name
age = 16


Here's what the resulting file looks like in a hex editor:

http://imgur.com/XTRcZco

The highlighted section is the "age", whereas the rest (black text only) is the "name"

Note what you're doing here:
fbin.write(name, sizeof(name)-1);

sizeof(name) is going to be 20 because that's how big the array is. You then subtract 1 from that... so you're writing 19 bytes from the 'name' array to the buffer.

And if you look at the file in a hex editor... that's exactly what happened. The first 19 bytes in the file are "name", followed by the null terminator (value of 00), followed by a bunch of random garbage that was left uninitialized in the 'name' buffer.

You then do this:
fbin.write((char*)(&age), sizeof(int));

sizeof(int) is 4 on my machine. So this wrote 4 bytes (the highlighted section)

Since the age was 16... in hex.. in 4 bytes that would be 0x00000010
Stored little endian (low byte first), that's 10 00 00 00 ... which is exactly what's in the file.



If you open this in a text editor like notepad... you'll see something similar to what's in the right column there. 'name' followed by a bunch of garbage symbols (look kind of like Is). That's because text editors read the file... and interpret each byte as being the ascii code for a character.


So yeah. Use text editors for text files.
Use hex editor for binary files.
Last edited on
closed account (ivDwAqkS)
thanks Disch, but look at my image

http://i62.tinypic.com/30j0iky.png

why mine is something different like yours?
Last edited on
'name' is an array of 20 characters. You do not initialize that array... that means that each of those 20 characters is filled with random garbage.

On line 29 you do this:
cin.getline(name, sizeof(name)-1);

This does not necessarily fill the entire name array. Instead... it takes as much input as the user supplies and puts that in the array... leaving the remaining elements that uninitialized garbage.

So basically you have this:

1
2
3
4
5
char name[20];  // name="????????????????" where '?' is random garbage

// assuming user inputs "name" for their name:
cin.getline(name, sizeof(name)-1);  // name="name0???????????????"
   // where '0' is the null terminator, and '?' is random garbage 


So really... your picture is the same as mine. We both have "name" followed by the single null character. The only difference is the uninitialized garbage is different. Which... since it's "garbage", makes perfect sense.
closed account (ivDwAqkS)
thanks for your help :)
Topic archived. No new replies allowed.