Jump to content
  • Advertisement
Sign in to follow this  
alway616

What is wrong with this line?

This topic is 4241 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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 )

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!