用 CFileDialog 控件获取文件打开或保存的文件名。 b.自定义一个测量点数据结构体,其元素包括:点号,编码,X,Y,H c.用文本框显示原始数据和整理后的数据 d..按编程规范进行编码
时间: 2024-04-30 19:23:48 浏览: 74
VC文件操作大全,打开,保存,复制,删除,查找等.doc
5星 · 资源好评率100%
a. 使用CFileDialog控件获取文件名的代码示例:
```c++
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Text Files (*.txt)|*.txt|All Files (*.*)|*.*||"), NULL);
if (dlg.DoModal() == IDOK)
{
CString strFilePath = dlg.GetPathName();
// 处理文件名
}
```
b. 自定义测量点数据结构体的代码示例:
```c++
struct MeasurePoint
{
int pointNo; // 测量点号
CString code; // 编码
double x; // X坐标
double y; // Y坐标
double h; // 高程
};
```
c. 显示原始数据和整理后数据的代码示例:
```c++
// 原始数据
CString strRawData;
strRawData.Format(_T("%d %s %.3f %.3f %.3f\r\n"), measurePoint.pointNo, measurePoint.code, measurePoint.x, measurePoint.y, measurePoint.h);
m_editRawData.SetWindowText(strRawData);
// 整理后数据
CString strCleanData;
strCleanData.Format(_T("%d,%s,%.3f,%.3f,%.3f\r\n"), measurePoint.pointNo, measurePoint.code, measurePoint.x, measurePoint.y, measurePoint.h);
m_editCleanData.SetWindowText(strCleanData);
```
d. 编程规范要求代码风格整洁、命名规范规范、注释清晰等,以下是示例代码的一个完整示例:
```c++
// 定义测量点数据结构体
struct MeasurePoint
{
int pointNo; // 测量点号
CString code; // 编码
double x; // X坐标
double y; // Y坐标
double h; // 高程
};
// 打开文件对话框获取文件名
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Text Files (*.txt)|*.txt|All Files (*.*)|*.*||"), NULL);
if (dlg.DoModal() == IDOK)
{
// 获取文件名并打开文件
CString strFilePath = dlg.GetPathName();
FILE* fp = _wfopen(strFilePath, _T("r"));
if (fp != NULL)
{
// 读取文件内容并解析数据
CString strLine;
while (fgetws(strLine.GetBufferSetLength(1024), 1024, fp) != NULL)
{
MeasurePoint measurePoint;
int nFields = _stscanf_s(strLine, _T("%d %s %lf %lf %lf"), &measurePoint.pointNo, measurePoint.code.GetBufferSetLength(64), &measurePoint.x, &measurePoint.y, &measurePoint.h);
if (nFields == 5)
{
// 原始数据
CString strRawData;
strRawData.Format(_T("%d %s %.3f %.3f %.3f\r\n"), measurePoint.pointNo, measurePoint.code, measurePoint.x, measurePoint.y, measurePoint.h);
m_editRawData.SetWindowText(strRawData);
// 整理后数据
CString strCleanData;
strCleanData.Format(_T("%d,%s,%.3f,%.3f,%.3f\r\n"), measurePoint.pointNo, measurePoint.code, measurePoint.x, measurePoint.y, measurePoint.h);
m_editCleanData.SetWindowText(strCleanData);
}
}
// 关闭文件
fclose(fp);
}
}
```
阅读全文