| |

VerySource

 Forgot password?
 Register
Search
View: 927|Reply: 5

Ask about file opening and saving data operations: use serialization

[Copy link]

2

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 Invalid IP Address

Post time: 2020-10-21 14:30:02
| Show all posts |Read mode
Problem summary: serialization saves double-precision data, decimal, newline, space
The code is as follows, my data is saved in a collection class m_SelPtList, which has three member variables (respectively X, Y, Z three coordinate values, the type is double), what I saved in this way is a binary format file, I would like to ask how to convert to decimal display format (that is, what is saved in the file is decimal).
CFile file("1.txt",CFile::modeCreate | CFile::modeWrite);
CArchive ar(&file,CArchive::store);
int iCount=Spt.m_SelPtList.GetSize();
for(int i=0;i<iCount;i++)
{
ar<<m_SelPtList[i]->x<<m_SelPtList[i]->y<<m_SelPtList[i]->z;
}
In the file, I hope to save it as follows: each line represents a point, and the coordinates of each point are separated by spaces to store the X, Y, and Z coordinates. As shown below:
-2.372417e+02 -4.032650e+01 -5.574990e+01//click one
-2.260712e+02 -4.186780e+01 -5.933653e+01//point two
How can I save in a new line in serialization? A space separates each value? How to write a better code for opening this file?
Reply

Use magic Report

2

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 Invalid IP Address

 Author| Post time: 2020-10-21 15:45:01
| Show all posts
//The dumbest way?
CFile file("1.txt",CFile::modeCreate | CFile::modeWrite);
char chKG[2]=" ";//space
char chHH[1]={10};//Enter
int iCount=m_SelPtList.GetSize();//Total number of data to be saved
for(int i=0;i<iCount;i++)
{
double vex_x[1]={Spt.m_SelPtList[i]->x};//Take out the first point
double vex_y[1]={Spt.m_SelPtList[i]->y};//Take out the second point
double vex_z[1]={Spt.m_SelPtList[i]->z};//Take out the third point
file.Write(vex_x,sizeof(vex_x[0]));//Save the first point
file.Write(chKG,2);//space
file.Write(vex_y,sizeof(vex_y[0]));//Save the second point
file.Write(chKG,2);//space
file.Write(vex_z,sizeof(vex_z[0]));//Save the third point
file.Write(chHH,2);//Enter
}
file.Close();

But what I save like this is still a binary format file. I want to save it as a decimal format file. How do I do it?
Reply

Use magic Report

0

Threads

15

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 Korea, Republic of

Post time: 2020-10-21 16:00:01
| Show all posts
It can be done using CString::Format()

double value = 123456.6789;

CString strValue;
strValue.Format("%le ", value);

Then save strValue to a file.
Reply

Use magic Report

0

Threads

25

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-10-21 17:00:01
| Show all posts
I want to save as a file in decimal format
===========

What you want is a text file.

In addition, your Serialize is best placed in the collection class m_SelPtList.
Reply

Use magic Report

0

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-10-21 17:45:01
| Show all posts
You can format the data to be written with a CString object,
Then use ar to write the CString object to the file
Reply

Use magic Report

2

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 Invalid IP Address

 Author| Post time: 2020-10-21 18:45:01
| Show all posts
Thank yousky_hexia,yctoyaandzrl8668
Another carriage return use this: char chHH[5]="\r\n";//Enter
The problem is solved, cool :) Thanks again!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list