What is wrong with this line?

Started by
7 comments, last by RAZORUNREAL 17 years, 4 months ago
What is wrong with this line of code? fout.write((string*) (&password), sizeof(password)); password is a string and im trying to store it in binary format [Edited by - alway616 on December 11, 2006 6:22:00 PM]
Advertisement
std::ofstream::write doesn't take a string as its parameter, and it certainly doesn't take a pointer to a string.

fout.write(password.c_str(), password.size());, assuming password is of type std::string. The code you have suggests that you learned some variant of C++ from a book that focused on teaching you C idioms, such as null-terminated character arrays as strings.

Alternatively, replace "string" with "char" in your code.
Thanks Ill try that
btw I learned my file I/O from the article on this site about it :)


[Edited by - alway616 on December 11, 2006 6:57:13 PM]
how would i do input then?
if it try this same method it makes an error
"invalid conversion from 'const char*' to 'char*'"

it will compile with:
fin.read((char*)(&password),sizeof(password));

but when it runs the program crashes! (my guess is because of trying to convert the string to a char )
post the declaration of the variable "password".

-me
password is a global string, i think i might know the problem, in a min ill see if my solution works or not

edit: nope
You cannot write an std::string into a file just like that. The string object will most probably not contain the actual text data, but instead have a pointer to somewhere else (probably a dynamically allocated piece of memory). This means that the actual text *is not* stored if you try to use write() like that.

In general, only POD types that don't contain pointers can be written to a stream like that (std::string is certainly not a POD type); others require more sophisticated handling. You might take a look at this to learn more about serialization. Boost.Serialization is one easy-to-use solution.
Quote:Original post by alway616
password is a global string, i think i might know the problem, in a min ill see if my solution works or not


Post the actual code that declares the password variable. "global string" can mean a couple different things:

const char *password = "funtimes";
const std::string password("funtimes");

We can't help you unless you tell us this.

-me

The problem is that read and write expect a character array, as in char*, not a string. Oluseyi suggested you use password.c_str(). This function returns a const char* to what's in the string, which is what write is expecting. The problem with using it in the read function is that it's const, i.e. read only. So the read function can't overwrite your strings data.

I can think of a couple of options. If your string doesn't have spaces in, you should be able to simply use:
std::string password;fin >> password;

If you must use write/read, this might work (I say might because it's untested and a bit dodgy):
const int readLeng = 256; //Bad - who knows how big the password is?char passwordBuffer[readLeng];fin.read(passwordBuffer, readLeng);std::string password(passwordBuffer);
___________________________________________________David OlsenIf I've helped you, please vote for PigeonGrape!

This topic is closed to new replies.

Advertisement