0
struct data {
    string name;
    string last_name;
    string description;
    string key_words;
    string title;
    int age;
};
data data1;

void display_file(){

   ifstream binaryFile;
   
   int size = 0;
   
   binaryFile.open("data.dat",ios::in | ios::binary);
   binaryFile.seekg(0,ios::end);
   size = (int)binaryFile.tellg();
   binaryFile.seekg(0,ios::beg);
   
   while(binaryFile.tellg() < size){        
        binaryFile.read((char*) &dane1, sizeof(dane));     
        cout << name << last_name << description << key_words << title << age;
   }

// save to file

ofstream plik;
    
    plik.open("data.dat",ios::out | ios::binary | ios::app);
if(plik.is_open()){
        
        
        plik.write((const char *) &data1, sizeof(data));
        
        plik.close();

    }

Hello in other method i had get_data to input all this values to binary file and in this method I want to read from binary file. The problem is that when I try to run this method program shows me only integers and in place where string value supposed to be i get garbage data. I know this method can read char array[] strings, but is this possible to read std::string in c++?

  • 2
    Assuming that `dane1` is actually `data1` then you simply cannot read in data like that. `data` is not a trivial type, you cannot just treat it as "raw memory" and expect things to work – UnholySheep May 11 '22 at 20:26
  • 2
    You can't read `std::string`s from binary files likes that. Read about [serializing](https://stackoverflow.com/questions/1809670/how-to-implement-serialization-in-c) – Ted Lyngmo May 11 '22 at 20:26
  • 1
    you'd have to show how file was saved. If it was saved similarly, you probably saved values that pointed to memory at the time it was saved, but not the contents of that memory, so the file is useless. – Garr Godfrey May 11 '22 at 20:28
  • `std::string` is a complicated beast. At the very simplest, it is a pointer to a buffer of data that sits elsewhere in memory and a book-keeping variable that tracks the length of the string in the buffer. write that into a file with a simple function of write and all you write is the value of the pointer, an address, and the length. None of the data in the pointed-at buffer was written. – user4581301 May 11 '22 at 20:32
  • cont.: If you are "unlucky" you may actually be able to write down and read back `std::string` s like this and then you may think that we're wrong. There's a thing called SSO (short string optimization) where short strings are actually stored inside the string object and you may be able to write and read those like you try. If you've noticed this behavior, just try with longer strings and then you'll see that it breaks. – Ted Lyngmo May 11 '22 at 20:44
  • SSO may still be fatal, sooner or later. The SSO buffer will be in the information written to the file and in the new copy of the string read back out of the file, but the address of the buffer read out of the file will still be the address of the original `string`'s SSO buffer, not the copy's SSO buffer, and the original may no longer exist and, if it does exist, could disappear at any point in the future leaving the copied `string` with a dangling pointer. And that dangling pointer may still appear to work until the system reclaims or reuses the storage. This gets nasty. – user4581301 May 11 '22 at 20:55

0 Answers0