Writing and reading a string to a C ++ binary

I'm having trouble writing a string to a binary. 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 can't get it to work. I'm sorry, I just despaired. Thanks!

+4
source share
6 answers

line

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

wrong

sizeof(string) does not return the length of the string; it returns sizeof the string type in bytes.

also does not discard text in char* using C cast, you can get char * using the corresponding member function text.c_str()

you can just write

 outfile << text; 

instead.

+7
source

1) why do you use pointers for the string class? 2) you should not use sizeof with a string, it returns the size of the object, not the size of the string.

You must try:

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

or

 outfile<<text; 
+2
source

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

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

To read it, cancel the process by first resizing the line so that you have enough space:

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

Since strings are of variable size, if you did not put this size in the file, you cannot get it correctly. You can rely on the token '\ 0', which is guaranteed to be at the end of line c or the equivalent line :: c_str (), but this is not a good idea, because 1. you must read the character that checks the value in the string character null 2. a std :: string can legitimately contain a null byte (although this is really not the case because the calls to c_str () are then confused).

+1
source

Perhaps also use c_str() to get a char pointer, and not just a crazy translation.

0
source

Try this piece of code.

 /* 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.

0
source

Your code is incorrect incorrectly, as you use to write and read the file and the file extension error you are trying to read the 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 it. It will work.

0
source

Source: https://habr.com/ru/post/1415852/


All Articles