hi,
I've just done something like below in c++: - ON - WINDOWS 10
   //1. Serialize a number into string then write it to file
        std::string filename = "D:\\Hello.txt";
        size_t OriNumber = 26;
        std::string str;
        str.resize(sizeof(size_t));
        memcpy(&str[0], reinterpret_cast<const char*>(&OriNumber), str.size());
        std::ofstream ofs(filename);
        ofs << str << std::endl;
        ofs.close();
        //2. Now read back the string from file and deserialize it
        std::ifstream ifs(filename);
        std::string str1{std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>()};
        // 3. Expect that the string str1 will not be empty here.
        size_t DeserializedNumber = *(reinterpret_cast<const size_t*>(str1.c_str()));
        std::cout << DeserializedNumber << std::endl;
At step 3, I could not read the string from file, even if I opened the file with notepad++, it showed several characters. At last line we still have the value of DeserializedNumber got printed, but it is due to str1.c_str() is now a valid pointer with some garbage value.
After debugged the program, I found that std:ifstream will get the value -1(EOF) at beginning of the file, and as explanation of wikipedia, 26 is value of Substitue Character and sometime is considered as EOF.
My question is:
- if I can't read value 26 from file as above, then how can serialization library serialize this value to bytes?
- and Do we have some way to read/write/transfer this value properly if still serialize the value 26 as my way above?
thanks,
