简体   繁体   中英

write and read string to binary file C++

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!

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.

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).
  • 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;

也应该使用c_str()来获取字符指针,而不是直接的疯狂转换。

Your code is wrong wrong way you are using to write & read the file and file extension error you are trying to read text file .txt correct code

Write to file

std::string text = "Text";
ofstream outfile("myfile.dat", ofstream::binary | ios::out);
outfile.write(&text,sizeof (string));//can take type
outfile.write(&text,sizeof (text));//can take variable name
outfile.close();

reading file

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

Try This it will work

I had the same problem. I found the perfect answer here: Write file in binary format

Key issues: use string::length to get the length of the string when writing out and use resize() before reading the string. And both for reading and writing, use mystring.c_str() instead the string itself.

Try this code snippet.

/* writing string into a binary file */

  fstream ifs;
  ifs.open ("c:/filename.exe", fstream::binary | fstream::in | fstream::out);

  if (ifs.is_open())
  {
   ifs.write("string to binary", strlen("string to binary")); 
   ifs.close();
  }

Here is a good example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM