The article is similar to my old post C++: Save Data To A Local File That Humans Can Read.
I want to save a little data into a local file that human can’t read it. It is best done conveniently through C Plus Plus.The following example shows the entire logic.

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

std::string filePath = "txt";

void WriteData()
{
    double origin[3] = { 0, 0, 0 };
    double scale[3] = { 1, 1, 1 };
    double orientation[3] = { 0, 0, -6.84607 };
    double position[3] = { -0.33, -4.7, 0.8 };

    ofstream output;
    output.open( filePath.c_str(), ios::binary );
    if( output.is_open() == false )
    {
        cout << "open failed: " << filePath.c_str() << endl;
        return;
    }

    for( int i = 0; i < 3; ++i )
    {
        output.write( (char*)&origin[i], sizeof( origin[i] ) );
    }

    for( int i = 0; i < 3; ++i )
    {
        output.write( (char*)&scale[i], sizeof( scale[i] ) );
    }

    for( int i = 0; i < 3; ++i )
    {
        output.write( (char*)&orientation[i], sizeof( orientation[i] ) );
    }

    for( int i = 0; i < 3; ++i )
    {
        output.write( (char*)&position[i], sizeof( position[i] ) );
    }
    char buffer[20] = "finished";
    output.write( buffer, sizeof( buffer ) );

    output.close();
    cout <<  "finished\n";
}


void ReadData()
{
    ifstream input;
    input.open( filePath.c_str(), ios::binary );
    if (input.is_open() == false)
    {
        cout << "open failed: " << filePath << endl;
        return;
    }
    std::string str = "\t";
    double origin[3];
    for( int i = 0; i < 3; ++i )
    {
        input.read( (char *)&origin[i], sizeof( origin[i] ) );
        cout << origin[i] << str;
    }
    cout << endl;

    double scale[3];
    for( int i = 0; i < 3; ++i )
    {
        input.read( (char *)&scale[i], sizeof( scale[i] ) );
        cout << scale[i] << str;
    }
    cout << endl;

    double orientation[3];
    for( int i = 0; i < 3; ++i )
    {
        input.read( (char *)&orientation[i], sizeof( orientation[i] ) );
        cout << orientation[i] << str;
    }
    cout << endl;

    double position[3];
    for( int i = 0; i < 3; ++i )
    {
        input.read( (char *)&position[i], sizeof( position[i] ) );
        cout << position[i] << str;
    }
    cout << endl;

    char buffer[20];
    input.read( buffer, sizeof( buffer ) );

    if( strcmp(buffer, "finished") == 0 )
    {
        cout << "Read successfully!\n";
    }

    input.close();
}

int main()
{
    WriteData();
    ReadData();
    return 0;
}

Output:

finished
0       0       0
1       1       1
0       0       -6.84607
-0.33   -4.7    0.8
Read successfully!

The local file:

Categories: CPlusPlus

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments

Tex To PDF
: convert the Latex file which suffix is tex to a PDF file

X
0
Would love your thoughts, please comment.x
()
x