Home>

I am writing a program that reads a binary file and converts it to a text file format.

As an example, assume the following elements are represented in order from the beginning of the binary file.
・ 4byte is an unsigned integer: Apple size
・ 4byte is an unsigned integer: Apple's weight
・ 64 bytes are unsigned characters: Apple varieties

Process flow
① Read file open
(2) Store the binary file in the member variable of the internal class.← Implementing here
③ Shift the variable stored in ② or convert from hexadecimal to decimal
④ Write the value converted in ③ to a text file with the write function← Implementing here


I want to solve now
Store binary data value in member variable of inner class,
Write using the wite method of the main class (class that performs write processing)
I want to do the processing, but I get an error below.

The class needs to be initialized ... but I'm having trouble understanding it.
I would appreciate your teaching.

Error message
// Write to file
void MAIN :: Write ()
    OutFile<<main.Size<<endl;
Run-Time Check Failure # 3-The variable 'datainfo_head' is being used without being initialized.</Code></pre>
<strong>Applicable source code</strong>
<pre><code>/*** Main class ** main.h *** /
    class MAIN {
      // inner class
    public:
        class Apple;
        class Orange;// Has approximately the same member variables as apple, but is now excluded
    private:
        std :: ifstream file;
        std :: ofstream OutFile;
    public:
        bool INFile_Open ();
        bool INFile_Close ();
        void Read_Apple ();
        void Read_Orange ();
        bool OutFile_Open ();
        bool OutFile_Close ();
        void Write ();
Unsigned __int32 func_datahead (unsigned __int32 temp);
                // void Analysys ();// Data analysis

        / * Apple * /
        class APPLE {
        public:
            unsigned __int32 Size;
            unsigned __int32 Weight;
            char Variety [64];
        };
    };
/* Apple.cpp * /
# include&lt;iostream&gt;
# include&lt;fstream&gt;
# include&lt;string&gt;
#include&lt;sstream&gt;
# include&lt;cstring&gt;
#include "main.h"
using namespace std;

/ ******* ******* /
bool MAIN :: INFile_Open (char * FileName) {
    file.open (FileName, ios :: in | ios :: binary);
    cout&lt;&lt;"FileName:"&lt;&lt;FileName&lt;&lt;endl;
    Read_Apple ();
    return true;
}// Stored in hexadecimal, big endian
void MAIN :: Read_Apple () {
    MAIN :: APPLE apple;
    file.read ((char *)&amp;apple.Size, 4);// Example 0x 58 00 00 00
    file.read ((char *)&amp;apple.Weight, 4);
    file.read (apple.Variety, 64);
}
bool MAIN :: INFile_Close () {
    file.close ();
    return 0;
}
bool MAIN :: OutFile_Open ()
{
    OutFile.open ("C: \\ Users \\ Desktop \\ apple.txt", ios :: out);
    Write ();
    return true;
}
bool MAIN :: OutFile_Close ()
{
    OutFile.close ();
    return 0;
}
// write to file
void MAIN :: Write ()
{
    MAIN :: APPLE apple;
    OutFile&lt;&lt;"APPLE DATA"&lt;&lt;endl;
    cout&lt;&lt;"aaa";
    OutFile&lt;&lt;apple.Size&lt;&lt;endl;
}
// Added shift operation function
unsigned __int32 MAIN :: func_datahead (unsigned __int32 temp)
{
    temp = (temp&lt;&lt;24) | ((temp&lt;&lt;8)&amp;0x00ff0000) | ((temp&gt;&gt;8)&amp;0x0000ff00) | ((temp&gt;&gt;24)&amp;0x000000ff);
Return temp;
}
/* main.cpp * /
# include&lt;iostream&gt;
# include&lt;fstream&gt;
# include&lt;string&gt;
#include "RouteDump.h"
using namespace std;
int main ()
{
    MAIN main;
    char FileName [200];
    cout&lt;&lt;"Please specify the import file"&lt;&lt;endl;
    cin&gt;&gt;FileName;
    if (main.INFile_Open (FileName)! = true) {
        cout&lt;&lt;"File Open Erorr!"&lt;&lt;endl;
    }

    if (main.OutFile_Close ()! = true) {
        cout&lt;&lt;"File Open Erorr!"&lt;&lt;endl;
    }
    main.InFile_Close ();
    main.OutFile_Close ();
    return 0;
}
Supplemental information (FW/tool version etc.)

VIsualstudio2015 C ++ 11 or later

Append

Added 2018/8/29
Thanks for your answers.
Apparently, the current problem is that binary data can be obtained in apple.Size with Read_APPLE ()
When writing with Write (), I found that apple.Size has no value (eg 0xcccccccc).
But I don't know why the value disappears ...
I would appreciate it if you could teach me.

  • Answer # 1

    Hello.

    [C ++] Binary input/output of structures and variables is helpful.
    It's not very desirable to do it in C ++ because it's a C language way of thinking, but if you try to be like C ++, it will be difficult if you do not introduce boost :: serialization, cereal, Theolizer, etc. (Theolizer is made by me.)

  • Answer # 2

    Check the contents of the saved file

      

    OutFile<<apple.Size<<endl;

    While this is written as a string,
    It doesn't seem to be the case when reading


    Oh, that's how it looks. Then

      

    file.read ((char *)&apple.Size, 4);// Example 0x 58 00 00 00

    Is this&(apple.Size)?

  • Answer # 3

    Do you know constructors?

    This is the mechanism that initializes when creating an instance of class APPLE.

    Normally the initialization is as follows.
    In the example presented, you are trying to reference an uninitialized Size.

    MAIN :: APPPLE :: APPLE ()
    {
     self.Size = -1;
     self.Weight = -1;
     Variety [0] = '¥ 0';// skip
    }

  • Answer # 4

    Thank you for answering.
    From your answers, I was able to solve the problem with hints.

    Solution:
    In the header file, the class declaration is as follows.

    /****** main.h ****** /
    / * Apple * /
      class APPLE {
         public:
          unsigned __int32 Size;
          unsigned __int32 Weight;
          char Variety [64];
      };
    / *** Main class *** /
        class MAIN {
          // Declare the inner class This is the solution! apple.size can be used with "apple.cpp"
        public:
            Apple apple;
            Orange orange;has approximately the same member variables as // apple but is now excluded
        private:
            std :: ifstream file;
            std :: ofstream OutFile;
        public:
            bool INFile_Open ();
            bool INFile_Close ();
            void Read_Apple ();
            void Read_Orange ();
            bool OutFile_Open ();
            bool OutFile_Close ();
            void Write ();
    Unsigned __int32 func_datahead (unsigned __int32 temp);
                    // void Analysys ();// Data analysis
        };
    /****** apple.cpp ****** /
    void MAIN :: Read_Apple () {
        MAIN :: APPLE apple;
     Despite generating APPLE here,
     Since Write_Read () also declares MAIN :: APPLE apple;again,
     The contents of apple.Size were initialized and disappeared.
        file.read ((char *)&apple.Size, 4);// Example 0x 58 00 00 00
        file.read ((char *)&apple.Weight, 4);
        file.read (apple.Variety, 64);
    }