I'm trying to read .txt data (formatted with spaces) into a struct object. To simplify the question, say the .txt contains one record of a student's first name, last name, campus code, and id.
Here is the struct:
struct Student {
char f_name[10]; // John, followed by 6 spaces
char l_name[20]; // Fuller, followed by 14 spaces
char camp_code;
char id[8]; //
} s;
Here is how i use the reinterpret_cast with read from fstream:
f.read(reinterpret_cast<char *>(&s), sizeof(s));
It compiles, but when I'm displaying (edit: std::cout<<s.f_name; ...
) the result, the result looks like this:
first name: John Fuller E23123456
last name: Fuller E23123456
campus code: E
id: 23123456
It seems like the compiler successfully found the starting point of each component of the struct object, but it stores the entire record starting from that component. Except for the camp_code part, it correctly grabbed the char
element. Any idea where I have missed here?
Thanks!