C++ file operation (6)-read the contents of the file mark position

1. The role of the file configuration file

Under normal circumstances, we will use configuration files to configure some parameters required by the program. The purpose is to prevent these parameters from being written in the program to prevent the program from having to be recompiled every time the parameters are changed. The following is a demonstration of how to use a simple configuration file.

2. Demonstration of obtaining the content of the marked location of the configuration file

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("打开文件失败"));
}

File content:
Insert picture description here
Result:
Insert picture description here
Insert picture description here
Insert picture description here
You can see the content that can read the specified location of the configuration file.

Insert picture description here

Guess you like

Origin blog.csdn.net/baidu_41191295/article/details/112913534