C++文件操作(6)--读取文件标记位置内容

一. 文件配置档作用

通常情况下,我们会使用配置档来配置程式需要的一些参数,目的是为了,不将这些参数写死在程式中,防止每次参数变更时,程式都要重新编译。下面就演示一下简单配置档的使用方式。

二. 实例演示获取配置档标记位置内容

CString	LogPath;
CFileFind CF;
CString line;
LogPath="D:\\MFC Project\\FileTest.txt";
CStdioFile cf(_T(LogPath),CFile::modeRead);
CString name;
CString age;
if(CF.FindFile(LogPath))
{
	cf.Open(_T(LogPath),CFile::modeRead);
	AfxMessageBox(_T("打开文件成功"));
	while(cf.ReadString(line))
	{
		if(line=="[NAME]")
		{
			cf.ReadString(line);
			name=line;
		}
		if(line=="[AGE]")
		{
			cf.ReadString(line);
			age=line;
		}
	}
	cf.Close();
	AfxMessageBox(_T(name));
	AfxMessageBox(_T(age));
}	
else	
{
	AfxMessageBox(_T("打开文件失败"));
}

文件内容:
在这里插入图片描述
结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
可以看到能够读取配置档指定位置的内容。

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/baidu_41191295/article/details/112913534