Sign in to follow this  

What is wrong with this line?

This topic is 4018 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

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
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

This topic is 4018 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this