[MFC] 编辑框数据的存储和读取.TEXT

一、将编辑框数据存储到.TEXT文档中

    char szFilter[] = { "TXT Files (*.txt)|*.txt|Excel Files (*.xls)|*.xls||" };
	CString fileName;
	fileName = "*.*";
	CString PBFilePath;
	CFileDialog FileDlg(FALSE, "txt", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, NULL);
	if (IDOK == FileDlg.DoModal())
	{
		PBFilePath = FileDlg.GetPathName();
		FILE *f;
		int i = 0;
		f = fopen(PBFilePath, "wt");
		if (f == NULL)
		{
			printf("文件打开失败!\n");
			return;
		}
		else
		{
			printf("文件打开成功!\n");
		   //基本参数
		   CString FontColor,BoxThickness,CrossColor,DilatationFator;
	       GetDlgItem(IDC_COMBOCOLOR)->GetWindowText(FontColor);
		   GetDlgItem(IDC_ET_BOX_LINE_WIDTH)->GetWindowText(BoxThickness);
		   GetDlgItem(IDC_CO_CROSS_COLOR)->GetWindowText(CrossColor);		   
		   GetDlgItem(IDC_ET_DILATATION_COEFFICIENT)->GetWindowText(DilatationFator);
		   fprintf(f, "%s ", FontColor+"\n");
		   fprintf(f, "%s ",BoxThickness+"\n");
		   fprintf(f, "%s ",CrossColor+"\n");
		   fprintf(f, "%s ",DilatationFator+"\n");
    	   //距离参数
		   //基准边
		   CString DEV,DRECT,MEASURE;
		   GetDlgItem(IDC_EDIT_ERROR_FIRST)->GetWindowText(DEV);
		   GetDlgItem(IDC_COMBO_MEASURE_DIRECTION_FIRST)->GetWindowText(DRECT);
		   GetDlgItem(IDC_EDIT_MEASURE_BOX_NUMBER_FIRST)->GetWindowText(MEASURE);	   
		   fprintf(f, "%s ",DEV+"\n");
		   fprintf(f, "%s ",DRECT+"\n");
		   fprintf(f, "%s ",MEASURE+"\n");
		   printf("文件写入成功!\n");
			i = fclose(f);
			if (i == 0)
				printf("文件关闭成功!\n");
			else
				printf("文件关闭失败!\n");
		}
		if (PBFilePath == " ")
		{
			return;
		}
	}
	else
	{
		return;
	}

二、读取文本.TEXT的数据到编辑框中

CString m_contents,m_param0,m_param1,m_param2,m_param3,m_param4,m_param5,m_param6,m_param7,m_param8;
	CString fileName;
	char szFilter[] = { "TXT Files (*.txt)|*.txt|Excel Files (*.xls)|*.xls||" };
	CString fileName;
	fileName = "*.*";
	CString PBFilePath;
	CFileDialog FileDlg(TRUE, "txt", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, NULL);;
	//CFileDialog FileDlg(TRUE);
	if (IDOK == FileDlg.DoModal())
	{
	PBFilePath = FileDlg.GetPathName();
	CStdioFile m_file(PBFilePath,CFile::modeRead|CFile::typeText);//txt路径
	CStringArray m_data; 
	while(m_file.ReadString(m_contents))//每次读取一行;
	{
	   int idx=m_data.Add(m_contents);
		//afxDump << m_data[idx] << "\n";
	}
	m_param0.Format(_T("%s"),m_data[0]);
	GetDlgItem(IDC_COMBOCOLOR)->SetWindowText(m_param0);
	m_param1.Format(_T("%s"),m_data[1]);
	GetDlgItem(IDC_ET_BOX_LINE_WIDTH)->SetWindowText(m_param1);
	m_param2.Format(_T("%s"),m_data[2]);
	GetDlgItem(IDC_CO_CROSS_COLOR)->SetWindowText(m_param2);
	m_param3.Format(_T("%s"),m_data[3]);
	GetDlgItem(IDC_ET_DILATATION_COEFFICIENT)->SetWindowText(m_param3);
	m_param4.Format(_T("%s"),m_data[4]);
	GetDlgItem(IDC_EDIT_ERROR_FIRST)->SetWindowText(m_param4);
	m_param5.Format(_T("%s"),m_data[5]);
	GetDlgItem(IDC_COMBO_MEASURE_DIRECTION_FIRST)->SetWindowText(m_param5);
	m_param6.Format(_T("%s"),m_data[6]);
	GetDlgItem(IDC_EDIT_MEASURE_BOX_NUMBER_FIRST)->SetWindowText(m_param6);
	m_param7.Format(_T("%s"),m_data[7]);
	GetDlgItem(IDC_EDIT_ERROR_SECOND)->SetWindowText(m_param7);
	}
	else
	{
		return;
	}

注:
1)上面函数中 fopen直接用可能有点问题 ,解决方法:
        解决方案,项目 =》属性 =》c/c++ =》预处理器=》点击预处理器定义,编辑,加入_CRT_SECURE_NO_WARNINGS,即可。

2)CFileDialog FileDlg(FALSE, “txt”, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, NULL);
参数:FALSE:保存文档
参数:TRUE:打开文档

参考:https://bbs.csdn.net/topics/392044509?list=lz

    原文作者:丶布布
    原文地址: https://blog.csdn.net/weixin_43197380/article/details/100103700
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞