-1

I need to save a struct in a bin file and my doing that as below:

struct InputData inputData;
//do something
fileName = "xyz.bin";
fstream data_file(fileName.c_str(), ios::out|ios::binary);
data_file.write(reinterpret_cast<char *>(&inputData), sizeof(InputData));
data_file.flush();
data_file.close();

Now, I'm able to open it using a C++ code for reading it but how do I open it in a text editor or something else where I can see the values of each data member?

Thanks

EDIT:

Here's what inputData looks like:

struct InputData
{
    double JE_user;
    double CA_FR;
    double kz_user;
    double kzt_user;
    double kd_user;
    double basic_v_user;
    double JE_roof_user;
    double Roof_Thk_user;

    int Shell_Material_No[15][4];
    int Option_walkway;
    int curb_angle_No_user;
    int Option_PWG;
    int stfr_Mat_No[45];
    int stfr_Sec_No[45][2];


    int No_of_ShellCourses;
    int No_of_Stiffeners_R;
    int Shell_Appendx_No;

    double weight_stair_user;
    double weight_adder_user;
    double Av;

    int Roof_Material_No;
    double snow_load_bal;

};
Abhishek Arya
  • 413
  • 4
  • 13
  • 2
    Please post `InputData`. None of the file handling code will work if `InputData` is not a POD type. – PaulMcKenzie Jun 21 '18 at 12:53
  • What's POD? Can you explain? – Abhishek Arya Jun 21 '18 at 12:55
  • @AbhishekArya Can you post the code he asked for, [in your question](https://stackoverflow.com/posts/50969048/edit)? – WhozCraig Jun 21 '18 at 12:56
  • @AbhishekArya Did you try searching? https://stackoverflow.com/questions/146452/what-are-pod-types-in-c – Algirdas Preidžius Jun 21 '18 at 12:56
  • Edited my question – Abhishek Arya Jun 21 '18 at 12:58
  • 1
    @AbhishekArya - ok, right now your struct is POD. However, and this is important, if you update your `struct` to not make it a POD type, your entire file handling code will be broken. Better to properly *serialize* each member, and not just a gigantic blob of bytes to a file. – PaulMcKenzie Jun 21 '18 at 13:00
  • Also [see this pod tester program](http://coliru.stacked-crooked.com/a/873255112faf7203). Note that `1` is printed, so right now you're safe. But if you add, say, a non-default constructor, or a non-POD member like `std::string`, then you would get a `0` returned, and then your file handling code will not be correct. – PaulMcKenzie Jun 21 '18 at 13:06
  • Thanks @PaulMcKenzie for your suggestion. – Abhishek Arya Jun 21 '18 at 13:06
  • Thanks for the POD tester program. Now, I understood why was I unable to store a string in this bin file :) – Abhishek Arya Jun 21 '18 at 13:09

1 Answers1

1

how do I open it in a text editor or something else where I can see the values of each data member?

You need an Hex viewer to see the raw values. Or you can create a script to see the values into their real shape.

  • Thanks @jose. I tried opening it using https://www.onlinehexeditor.com/ but its not working the way I want it to be. Can you suggest a hex viewer? – Abhishek Arya Jun 21 '18 at 13:05
  • I prefer not to bias your preferences. There are plenty of them in windows, mac or even linux tools to see the hex dump. Look for hex editor on google and good luck! – José Manuel Ramos Jun 21 '18 at 13:09
  • So, basically I was able to open the bin file but it's all binary jus 0s and 1s. I thought I will be able to see every data member's name along with it's value. Seems like that's not possible. Or is it? – Abhishek Arya Jun 21 '18 at 17:28