I want to save a little data into a local file that we 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 << origin[i] << "\t";
}
output << std::endl;
for( int i = 0; i < 3; ++i )
{
output << scale[i] << "\t";
}
output << std::endl;
for( int i = 0; i < 3; ++i )
{
output << orientation[i] << "\t";
}
output << std::endl;
for( int i = 0; i < 3; ++i )
{
output << position[i] << "\t";
}
output << std::endl;
output << "finished";
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 >> origin[i];
cout << origin[i] << str;
}
cout << endl;
double scale[3];
for( int i = 0; i < 3; ++i )
{
input >> scale[i];
cout << scale[i] << str;
}
cout << endl;
double orientation[3];
for( int i = 0; i < 3; ++i )
{
input >> orientation[i];
cout << orientation[i] << str;
}
cout << endl;
double position[3];
for( int i = 0; i < 3; ++i )
{
input >> position[i];
cout << position[i] << str;
}
cout << endl;
input >> str;
if( str == "finished" )
{
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:
[…] 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 […]