Binary problem

Hello i am a new c++ user and i got a little problem using the binary write.
I m trying to transform a string into a binary but first of all the string stay a string and it only show the 4 first letter.

Here is my script and just to tell u the string is pretty big.
this fonction is storing a large number of data used in a game i m building but i was building it with a txt file first and got files of 32 mb so i wanted to reduce memory needed.

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <cstring>

using namespace std;

int main()
{
    int x =0;
    int y =0;
    int z =0;
    int cube_material =0;

    int pourcent = 0;
    int secteur_x =1;
    int secteur_y =1;
    std::string s;
    std::stringstream out;
while(secteur_y < 2)
{


while(z < 800)
{

        for(int i = 0; i <= 50 ; i++)
        {
        // convert in to string

        out << x;
        out << "-";
        out << y;
        out << "-";
        out << z;
        out << "-";
        out << secteur_x;
        out << "-";
        out << secteur_y;
        out << "/";
                x++;

}
            if(x >= 50)
            {
                y++;
                x = 0;
               // pourcent++;
               //     cout << "Loading cube: " << pourcent<< "%" <<endl;



                if(y >= 51)
                {
                    //z++;
                    y = 0;
                    x = 0;
                    z++;


                }
            }






}
cout << "Done"  <<endl;
////////////////////////////////////////////////////////////////
        s = out.str();
char * text_cube = new char [100000000];
strcpy(text_cube, s.c_str());

/////////////////////////////////////////////

string s2;
stringstream out2;

out2 << secteur_x;
out2 << "-";
out2 << secteur_y;
out2 << ".bin";
s2 = out2.str();

char * text_name = new char [100];
strcpy(text_name, s2.c_str());


        ofstream outbin( text_name , ios::out|ios::binary);

        if(outbin)
        {

               outbin.write( text_cube, sizeof(90) );


                outbin.close();
        }
        else
                cerr << "Fichier existant !" << endl;

secteur_y++;
cout << "Done 2"  <<endl;
}

}

What is your problem? Btw line 97 sizeof(90) does not make sense. What do you want to achieve?
my goal is to creat a binary file containing value of object (which are cube right now) and that would represent there position into a 3d world.

right now i am not able to transform my string into binary :S
i tryed lots of solution and searched and found nothing that could help me making it possible :S
what coder meant:
sizeof 90 doesn't make any sense. It's the same as sizeof int.
You don't need to copy 'out' to 'text_cube'

Replace line 97 outbin.write( text_cube, sizeof(90) ); -> outbin.write( out.str().data(), out.str().size());

Since sizeof(90) results to 4 you only write 4 bytes

Neither is it necessary to copy 's2' to 'text_name'.
Line 92 should be ofstream outbin( s2.c_str(), ios::out|ios::binary);
Last edited on
alright all those code are working great thank you.
But i still got the same problem (it show like if i was writing a text file and keep the same size which is 27 mb.

Wasnt it supose to transform all those text into binary character?
coder777 wrote:
You don't need to copy 'out' to 'text_cube'
One of the good things about binary files is the random access. To acomplish that all the registers should have the same size.
That is the purpose of the copy

@firestat: You are converting all your data to a string representation before writing it. The result will be the same as if it was plain text.
firestat wrote:
i wanted to reduce memory needed
ok, that's your main concern. You cannot achieve that through simple copy it to 'binary'.

two ways:

1. find out redundancies and remove them
2. use compression like 7 zip: http://www.7-zip.org/
Alright i had one compression system but wasnt able to use it.
Thx for the help!
I Will find tutorial on how to use compression system and reading those compression right after. :)
Thx for the help.
Topic archived. No new replies allowed.