Thứ Năm, 10 tháng 12, 2015

Read - write file.txt in C++

We have the located file with name:   c:/input_data/matrix.txt, data1.txt, data2.txt….
Ex:
matrix.txt:        m00 m01 m02
                        m10 m11 m12
                        m20 m21 m22
C++:
Read 1 file matrix.txt:
CMatrix intrinsic_matrix(4);                                           //create matrix 4x4

string datain_file = “c:/input_data/ matrix.txt”;                  //direction of file
string sline;
istringstream ssline;
ifstream matrix_file;

matrix_file.open(datain_file);
if (!matrix_file.data()){
            cout << " matrix_file was not found!" << endl;
            exit(0);
}
for (auto i(0); i < 3; i++){
            getline(matrix_file, sline);
            ssline.str(sline);                                                      //Convert to stream
            ssline >> intrinsic_matrix(i, 0) >> intrinsic_matrix(i, 1) >> intrinsic_matrix(i, 2);
            ssline.clear();
}
matrix_file.close();

//-----------------------------------------------------------------------------------------------
data1.txt:          r00 r01 r02 t0,  data2.txt …        //include Rotation matrix and translation vector
                        r10 r11 r12 t1
                        r20 r21 r22 t2

Read all file data1.txt … datak.txt:             // k file data.txt

CMatrix extrinsic_matrix(4);                            //create matrix 4x4
CMatrix Rotation(3);                                        //Rotation matrix
CMatrix Rotation_Transpose(3);                      //Transposed matrix of the rotation matrix
CVector Translation(3);                                    //Translation vector

ostringstream RT_file;    
string sline;
istringstream ssline;
ifstream datafile;

for (auto i(0); i < k; i++){
      RT_file << “c:/input_data/ data”<< k << txt”;     //direction of file

datafile.open(RT_file.str());

if (!RT_file.str().data()){
       cout << " RT_file was not found!" << endl;
       exit(0);
}

for (auto i(0); i < 3; i++){
       getline(datafile, sline);
       ssline.str(sline);                                                      //Convert to stream
       ssline >> extrinsic_matrix(i,0) >> extrinsic_matrix(i,1) >> extrinsic_matrix(i,2);
       ssline.clear();
}
datafile.close();

 //It is necessary in order to homogeneous coordinates
intrinsic_matrix(3, 3) = 1.0; //fourth column,fourth row :1
extrinsic_matrix(3, 3) = 1.0; //fourth column,fourth row :1

for (int x(0); x<3; x++){
       Translation[x] = extrinsic_matrix(x, 3);                        //only 3rd column
       for (int y(0); y<3; y++){
                   Rotation(x, y) = extrinsic_matrix(x, y);
       }
}

//To return the initial state
RT_file.str(empty_string);
RT_file.clear();

}

Write to file .txt

CVector3d* Up_Camera;   //output

string camerafile = "D://output_file/Up_Camera_Position.txt";
ofstream outfile(camerafile);

for (auto i(0); i < k; i++){
                                                     //calculate the Up_Camera.x,y,z and then save:
….
outfile << Up_Camera[k].x << " " << Up_Camera[k].y << " " << Up_Camera[k].z << endl;


}

Không có nhận xét nào:

Đăng nhận xét