reinterpret cast problem

I'm reading from a file and I came across this in a function.
While(infile.read(reiterpret_cast<char *>(&st),sizeof(Student)))
What does that reiterpret_cast<char *> mean ?
Student it's my class name.
Last edited on
reiterpret_cast<char *> is you telling the program that you want to treat the following variable (the address of st in this case) as if it were a char pointer.

Can we see some more code? I heavily suspect an over run or out of bounds error if I'm right and "st" is an std::string.
st is an object of type Student; &st is its address. ie. starting at the address of the byte at that address, the next sizeof(Student) bytes are occupied by the object st.

reiterpret_cast<char *>(&st) yields the address of the byte (char) at &st. We want to treat the memory that this Student object occupies as an array of sizeof(Student) bytes; we want to overwrite these bytes with the bytes read from the file.

This works (even though it is not a good way of performing i/o), if the bytes written into the file originally came from a Student object, we have not copied the file from one machine to another, and Student is a TriviallyCopyable type).
Assuming "infile" is open in binary mode and "sizeof(studen)" is a typo (lower case 's' and missing 't') I can see where you're going with that. It's an odd approach though; you would think someone teaching this would just copy an existing markup language and cover two topics with one lesson. Parsing text vs. reading raw data? I'd take the first one myself.
Topic archived. No new replies allowed.