0

I have been trying to read data from a binary file in C++, but I'm getting run time error, Program has stopped working!

I have used the similar code before and it is still working. I am getting an error while executing the constructor of the class SettingsClass [Maybe because of the read function, because after removing it, everything just ran great.]

struct Setting{
    int SettingID;
    int SettingINTValue;
    double SettingDOUBLEValue;
    char SettingCHARValue;
    string SettingSTRINGValue;
    string SettingName;
};

class SettingsClass {
  public:
    void ResetSettings() {
       fstream SettingFile;                                  
       Setting defaultsetting[NoOfSettings];

       for(int i=1;i<=NoOfSettings;i++) {
         defaultsetting[i-1].SettingID = i;
         defaultsetting[i-1].SettingINTValue = 0;
         defaultsetting[i-1].SettingDOUBLEValue = 0.0;
         defaultsetting[i-1].SettingCHARValue = '#';
         defaultsetting[i-1].SettingSTRINGValue = "null";
         switch(i) {
           default:
             defaultsetting[i-1].SettingName = "Compression Levels";
             defaultsetting[i-1].SettingSTRINGValue = "Normal";
             defaultsetting[i-1].SettingINTValue = 1;
             break;
         }
         cout<<i<<". "<<defaultsetting[i-1].SettingName<<"\n\t "<<defaultsetting[i-1].SettingINTValue<<"\n\t "<<defaultsetting[i-1].SettingDOUBLEValue<<"\n\t "<<defaultsetting[i-1].SettingCHARValue<<"\n\t "<<defaultsetting[i-1].SettingSTRINGValue<<"\n\t ";
         cout<<"\n";
       }

       SettingFile.open(SettingsFilePath,ios::binary|ios::out);
       if(SettingFile.is_open()){
         SettingFile.write(reinterpret_cast<char const *>(&defaultsetting),sizeof(defaultsetting));
       } else {
         cout<<"Error!";
       }
       SettingFile.close();                                   
    }

    SettingsClass() {
        fstream SettingFile;
        SettingFile.open(SettingsFilePath,ios::binary|ios::in);
        if(SettingFile.is_open()) {
          Setting TempSettings[NoOfSettings]; 
          SettingFile.read((char*)&TempSettings,sizeof(TempSettings)); 
        } else {
          cout<<"Error...";
        }
        SettingFile.close();
    }
} Settings;
2
  • 2
    please indent your code Commented Oct 21, 2013 at 17:50
  • You can't read and write std::string to a file like that and expect it to work. You need to serialize properly. Commented Oct 21, 2013 at 18:04

1 Answer 1

1

You should go read and learn more about file streams and the associated input and output operators << and >>. You cannot simply input characters into an array like this line of code:

SettingFile.read((char*)&TempSettings,sizeof(TempSettings)); 

The array is not of char and yet you cast it as such. Instead you should loop over the available input and fill in the array, e.g.:

for(size_t i = 0; i<NoSetting; ++i) {
  SettingFile >> TempSettings[i];
}

Of course you should overload the appropriate input operator:

istream& operator>>(istream& _is, Setting& _s) {
  //read all variables of setting, i.e.:
  //_is >> _s.var1;
  //_is >> _s.var2;
  //etc.
}

You likely have the same error for your output. You should overload:

ostream& operator<<(ostream& _os, const Setting& _s) {
  //output all variables of Setting, e.g.:
  //_os << _s.var1;
}

Do something like this instead of filestream write:

for(size_t i = 0; i<NoSetting; ++i) {
  SettingsFile << defaultSetting[i];
}
Sign up to request clarification or add additional context in comments.

6 Comments

Dude, I did the exact same thing as you said - First overload and then use it, but it helped to get rid of the run-time error, but still I got no data in my file!?
You should start a new question for the data not showing up in the file. But there are many examples of this online, make sure you follow it to the letter. Also try a smaller program practicing with ostream and istream. This will give you more experience to be able to implement them in a larger project.
Oh, sorry, it's working fine, I just used the wrong operator! Thanks Anyways.
Hey, but there is a problem with that retrieving data part, I'm getting garbage values! Please help me out.
Again I recommend a separate question now, as it is slightly different. And remember to post a minimal example of what doesn't work.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.