Can someone explain me this?

hi, sorry for my poor english.


I was coding this to see how the program reacted.


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
#include<fstream>
#include<iostream>
#include<array>
#include<string>

using namespace std;

struct objeto{
    
    string nombre;
    array<int,6> numeros;
    
};


void foo();


int main(int argc, char **argv)
{
    
    foo();

   
    ifstream sobjeto("objeto.ss", ios::binary);
    
    objeto var_2;
    
    sobjeto.read((char *)&var_2, sizeof(var_2));
    
    cout<<var_2.nombre<<endl<<endl;
 
    printf("hello world\n");

    return 0;
}


void foo(){
        ofstream seobjeto("objeto.ss", ios::binary);
    
        objeto variables;
        
        variables.nombre = "Moises";
        variables.numeros = {1,2,3,4,5,6};
        
        seobjeto.write((char *)&variables,sizeof(variables));
        seobjeto.close();
}





The output showed random characters appearing in my cmd window (and some beeps).

But then, curiously, I declared and defined a string within the main() function.




(SAME PROGRAM WITH A STRING DECLARATED AND DEFINED WITHIN MAIN)
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
#include<fstream>
#include<iostream>
#include<array>
#include<string>

using namespace std;

struct objeto{
    
    string nombre;
    array<int,6> numeros;
    
};


void foo();


int main(int argc, char **argv)
{
    
    foo();
    string a = "secreto";
    
    ifstream sobjeto("objeto.ss", ios::binary);
    
    objeto var_2;
    
    sobjeto.read((char *)&var_2, sizeof(var_2));
    
    cout<<var_2.nombre<<endl<<endl;
    
	printf("hello world\n");
	return 0;
}


void foo(){
        ofstream seobjeto("objeto.ss", ios::binary);
    
        objeto variables;
        
        variables.nombre = "Moises";
        variables.numeros = {1,2,3,4,5,6};
        
        seobjeto.write((char *)&variables,sizeof(variables));
        seobjeto.close();
    }


and everything ran perfectly.

Can someone explain me this? I dont get it.
You can't use the read()/write() function with a structure when you have non-trivial member variables like std::strings.

When writing a std::string using the write() method you'll write a pointer not the actual data. Plus since a string has a variable length you need to preserve the length of the string so you know how many bytes were written to the file.

When reading to a std::string the string must have enough room reserved to hold the data.

well, i can use everything within the same scope.

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
#include<fstream>
#include<iostream>
#include<array>
#include<string>

using namespace std;

struct objeto{
    string nombre;
    array<int,6> numeros;
};


int main(int argc, char **argv)
{
    ofstream seobjeto("objeto.ss", ios::binary);
    
    objeto variables;
        
    variables.nombre = "Moises";
    variables.numeros = {1,2,3,4,5,6};
        
    seobjeto.write((char *)&variables,sizeof(variables));
    seobjeto.close();
    string a = "secreto";
    
    ifstream sobjeto("objeto.ss", ios::binary);
    
    objeto var_2;
    
    sobjeto.read((char *)&var_2, sizeof(var_2));
    
    cout<<var_2.nombre<<endl<<endl;
    
	printf("hello world\n");
	return 0;
}


but that wasn't my question, my question is "why when I declare and define a string within the main() function, with the same program, everything runs perfect?"
It "works" because it's not printing what you think it is.

Try putting the following line at line 26:

variables.nombre = "SOMELONGERNAME";

By the way the above program crashes when I try to run it.
sorry for my bad english again.

This is weird, the above program (the one in the comments) don't crash on my pc, and I tested it with 3 diferent IDES.


Everything began when i wanted to test if there was an native and easy way to serialize objects (of course i was just playing with c++).

I thought that a string wouldn't work in this case. So i made the program above (in the comments) and it worked.

So I thought again "it may have worked because everything is in the same scope, it is saving a pointer to the string within the intance of the aggregate class i made".

So i wanted to see what happened if I saved the object within a file, and then, destroy the object (so the string frees), that's what I did and then, made the program that is in the top of this topic.

Everything went bad, (that's what I expected). but them I randomly put

string a = "secreto";//line 23

And it worked.

so, I wonder, why putting that within the code, makes someway the pointer stored in the file avaliable again?, i mean, that don't make sense at all.

did the string freed?
so, I wonder, why putting that within the code, makes someway the pointer stored in the file avaliable again?, i mean, that don't make sense at all.

Since you're not reading what you think you are it is pretty much pure luck. If, after you write the file the first time, you remove the write() call and re-run the program, you will possibly be able to see what is wrong. You're not writing the string to the file, you're writing the address of the string into the file.

Topic archived. No new replies allowed.