String/char question...

I am writing a binary file. This binary file is saying something like this:
binfil.read(reinterpret_cast<char*>(&n), sizeof(n));

Because of the char type used, this line of text dazes me.

I'm learning about the #include <string> functions...
what if i wanted to put reinterpret_cast<string>(&n), sizeof(n));
would that effect anything.

also whats the difference between char & string. Are they both 16 bits wide?
do they work the same way?

I'm not sure if I'm being ambiguous. If I am just ask, and I'll rephrase it.
would that effect anything.
Yes, it would stop working.

Whoever wrote that line cannot be trusted to write portable code.

A char is an integer type. std::string is a typedef for std::basic_string<char>. std::basic_string<T> is like an std::vector that also includes a few functions specific for string manipulation.
char is not guaranteed to be of any size, but all implementations I know of make it a signed 8-bit integer.
ok cool you answered my question, thank you.
Last edited on
Topic archived. No new replies allowed.