Problem while write in txt file

Hi, i'm having a problem while writing data in a txt file. My code is this:

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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>

using namespace std;
class students
{
    private:
    char _name[20];

    public:
    students();
    void sRegister();
};

students :: students()
{
    strcpy(_name, "");
}
void students ::  sRegister()
{
    cout << "Enter a student's name: " << endl;
    cin >> _name;

}

int main()
{
    ofstream fregistry ("students_list.txt");

    students reg1;

    reg1.sRegister();

    fregistry.write((char *)&reg1, sizeof(students));

    return 0;
}



The problem is:

after run my program, I input "Andre" with name, after this, I read the text file:

andre@wolfman:~/CPP$ cat students_list.txt
Andre$�������andre@wolfman:~/CPP$


What is " $������� "?

Thanks.
Here's a hint: on line 36, you write out sizeof(students) bytes. How many bytes is that?
Here's a hint: on line 36, you write out sizeof(students) bytes. How many bytes is that?


I used "sizeof" to inform a size of registry and after write in the text file.

File IO works only with POD types, so you can't write whole class to file. You can store data in struct:
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

struct student_data
{
  char name[20];
  // int age
  // ...
};

class students
{
    private:

    public:
    student_data data; 
    students();
    void sRegister();
};

students :: students()
{
    strcpy(data.name, "");
}
void students ::  sRegister()
{
    cout << "Enter a student's name: " << endl;
    cin >> data.name;

}

int main()
{
    ofstream fregistry ("students_list.txt",ios::binary); /// don't forget ios::binary flag!

    students reg1;
    reg1.sRegister();
    fregistry.write((char *)&reg1.data, sizeof(reg1.data));
    fregistry.close();

// check the output
    ifstream fregistry_in;
    fregistry_in.open("students_list.txt",ios::in | ios::binary);

    students reg2;
    fregistry_in.read((char *)&reg2.data, sizeof(reg2.data));
    fregistry_in.close();
    cout <<reg2.data.name;

    return 0;
}
File IO does not work well with POD types either. The size of "integer" "long" and "pointer" types, for example, vary across 8, 16, 32 and 64-bit platforms. The endianness of integral types varies across CPU families. And floating point types may be represented differently across platforms. All non-string types need to be explicitly serialized and de-serialized. And even strings are tricky -- you have to agree what character encoding is being used.

@andrezc: The garbage at the end of the file is because the full size of the _name member, all 20 bytes, are being written to the file, not just the 5 characters from the name "Andre". What you are seeing are the nul terminator and the uninitialized remnants of the _data array.
Topic archived. No new replies allowed.