Getting Garbage Values while reading struct data from a binary file

后端 未结 2 1023
无人及你
无人及你 2021-01-03 18:48

Hi guys in my previous question, I was able to get the data of a struct to be loaded on a file, but now the issue is I\'m getting garbage value while retrieving it.

相关标签:
2条回答
  • 2021-01-03 19:30

    It seems you forgot to load your setting into file.

    int main(int argc, char *argv[])
    {
        Settings.ResetSettings();  // insert code here
        Settings._SettingsClass();
        //    cout<<Settings.GetSetting(1).SettingName;
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    0 讨论(0)
  • 2021-01-03 19:38

    Formatted input requires delimiters so it knows when to stop reading for a particular value. Even though you're opening the file in binary mode what you're writing is in essence a text file with no delimiters so you can't read it back.

    If you must have a binary file then this is the way to read/write one using your structure:

    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <fstream>
    #include <vector>
    
    const char* SettingsFilePath = "settings.bin";
    
    struct Setting
    {
        int SettingID;
        int SettingINTValue;
        double SettingDOUBLEValue;
        char SettingCHARValue;
        std::string SettingSTRINGValue;
        std::string SettingName;
    
        Setting()
            : SettingID(0)
            , SettingINTValue(0)
            , SettingDOUBLEValue(0)
            , SettingCHARValue(0)
        {
        }
    
        void Write(std::fstream& out)
        {
            out.write(reinterpret_cast<const char*>(&SettingID), sizeof(SettingID));
    
            out.write(reinterpret_cast<const char*>(&SettingINTValue), sizeof(SettingINTValue));
    
            out.write(reinterpret_cast<const char*>(&SettingDOUBLEValue), sizeof(SettingDOUBLEValue));
    
            out.write(reinterpret_cast<const char*>(&SettingCHARValue), sizeof(SettingCHARValue));
    
            size_t str_size = SettingSTRINGValue.size();
            out.write(reinterpret_cast<const char*>(&str_size), sizeof(str_size));
            out.write(SettingSTRINGValue.c_str(), SettingSTRINGValue.size());
    
            str_size = SettingName.size();
            out.write(reinterpret_cast<const char*>(&str_size), sizeof(str_size));
            out.write(SettingName.c_str(), SettingName.size());
        }
    
        void Read(std::fstream& in)
        {
            in.read(reinterpret_cast<char*>(&SettingID), sizeof(SettingID));
    
            in.read(reinterpret_cast<char*>(&SettingINTValue), sizeof(SettingINTValue));
    
            in.read(reinterpret_cast<char*>(&SettingDOUBLEValue), sizeof(SettingDOUBLEValue));
    
            in.read(reinterpret_cast<char*>(&SettingCHARValue), sizeof(SettingCHARValue));
    
            size_t str_size;
            std::vector<char> str_data;
    
            in.read(reinterpret_cast<char*>(&str_size), sizeof(str_size));
            str_data.resize(str_size);
            in.read(&str_data[0], str_size);
            SettingSTRINGValue.assign(str_data.begin(), str_data.end());
    
            in.read(reinterpret_cast<char*>(&str_size), sizeof(str_size));
            str_data.resize(str_size);
            in.read(&str_data[0], str_size);
            SettingName.assign(str_data.begin(), str_data.end());
        }
    
        void Print(const std::string& title)
        {
            std::cout << title << "\n";
            std::cout << std::string(title.size(), '-') << "\n";
    
            const size_t w = 22;
            std::cout << std::setw(w) << std::right << "SettingID : " << SettingID << "\n";
            std::cout << std::setw(w) << std::right << "SettingINTValue : " << SettingINTValue << "\n";
            std::cout << std::setw(w) << std::right << "SettingDOUBLEValue : " << SettingDOUBLEValue << "\n";
            std::cout << std::setw(w) << std::right << "SettingCHARValue : " << SettingCHARValue << "\n";
            std::cout << std::setw(w) << std::right << "SettingSTRINGValue : " << SettingSTRINGValue << "\n";
            std::cout << std::setw(w) << std::right << "SettingName : " << SettingName << "\n";
    
            std::cout << "\n";
        }
    };
    
    int main()
    {
        {
            Setting s;
            s.Print("Default before Write");
    
            s.SettingID = 1;
            s.SettingINTValue = 2;
            s.SettingDOUBLEValue = 3.5;
            s.SettingCHARValue = 'Z';
            s.SettingSTRINGValue = "Blah Blah";
            s.SettingName = "Some Settings";
    
            std::fstream f(SettingsFilePath, std::ios::out | std::ios::binary);
            s.Write(f);
    
            s.Print("Values written to file");
        }
    
        {
            Setting s;
            s.Print("Default before read");
    
            std::fstream f(SettingsFilePath, std::ios::in | std::ios::binary);
            s.Read(f);
            s.Print("Values after read");
        }
    
        return EXIT_SUCCESS;
    }
    
    0 讨论(0)
提交回复
热议问题