write and read string to binary file C++

49,060

Solution 1

the line

outfile.write((char*) &text, sizeof (string));

is not correct

sizeof(string) doesn't return the length of the string, it returns the sizeof the string type in bytes.

also do not cast text to char* using a C cast, you can get hold of the char* by using the appropriate member function text.c_str()

you can simply write

outfile << text;

instead.

Solution 2

To write a std::string to a binary file, you need to save the string length first:

std::string str("whatever");
size_t size=str.size();
outfile.write(&size,sizeof(size));
outfile.write(&str[0],size);

To read it in, reverse the process, resizing the string first so you will have enough space:

std::string str;
size_t size;
infile.read(&size, sizeof(size));
str.resize(size);
infile.read(&str[0], size);

Because strings have a variable size, unless you put that size in the file you will not be able to retrieve it correctly. You could rely on the '\0' marker that is guaranteed to be at the end of a c-string or the equivalent string::c_str() call, but that is not a good idea because

  1. you have to read in the string character by character checking for the null
  2. a std::string can legitimately contain a null byte (although it really shouldn't because calls to c_str() are then confusing).

Solution 3

  • Why are you using pointers to std::string class?
  • You should not use sizeof with std::string, as it returns the size of the std::string object, and not the real size of the string inside.

You should try:

string text = "Text";
outfile.write(text.c_str(), text.size());

or

outfile << text;

Solution 4

Should probably also use c_str() to get the char pointer too, instead of that straight crazy cast.

Share:
49,060
Admin
Author by

Admin

Updated on July 16, 2021

Comments

  • Admin
    Admin almost 3 years

    Im having problems writing string into a binary file. This is my code:

    ofstream outfile("myfile.txt", ofstream::binary);
    std::string text = "Text";
    outfile.write((char*) &text, sizeof (string));
    outfile.close();
    

    Then, I try to read it,

    char* buffer = (char*) malloc(sizeof(string));
    ifstream infile("myfile.txt", ifstream::binary);    
    infile.read(buffer, sizeof (prueba));
    std::string* elem = (string*) buffer;
    cout << *elem;
    infile.close();
    

    I just cant get it to work. I am sorry, I am just desperate. Thank you!

  • chris
    chris almost 12 years
    Assuming it is std::string. The string is being used in a pretty weird way.
  • Sandeesh
    Sandeesh almost 7 years
    We appreciate your answer but this is a 5 year old question.
  • Scott Hutchinson
    Scott Hutchinson about 6 years
    This worked for me with one change. In the writing code, I changed outfile.write(&str[0],size); to outfile.write(str.c_str(),size);
  • Eyk Rehbein
    Eyk Rehbein about 3 years
    @Sandeesh I think this shouldn't matter as long as the answer is in a "okay-ish" format, which it is, from my point of view