C++ file operation (5)-use ReadString in MFC to read file content

This article mainly introduces the ReadString function in C++ to read the file content.

Claim

Given a file, determine whether there are certain characters in this file.

achieve

CString	LogPath="";
	CString line;
	//定义的输入框
	m_input=(CEdit*)(this->GetDlgItem(IDC_EDIT_DLG));
	
	CString Stmp;
	//获取输入框内容
	m_input->GetWindowText(Stmp);
	LogPath="D:\\MFC Project\\FileTest.txt";
	CStdioFile cf(_T(LogPath),CFile::modeRead);
	CString k;
	//设定标记flag
	bool flag=false;
	//读取文件
	while(cf.ReadString(line))
	{
		//如果读完文件可以找到需要的字符
		if(line.Find(Stmp)>=0)
		{
			AfxMessageBox(_T("OK"));
			AfxMessageBox(_T(line));
			flag=true;
			break;
		}
		k=line;
	}
	//如果标记是false
	if(!flag)
	{
		AfxMessageBox(_T("fail"));
		//输出最后一行字符
		AfxMessageBox(_T(k));
	}

Insert picture description here
Result:
Example 1
input character +, which exists in the file, and output this line of character string.
Insert picture description here
Insert picture description here
Example 2
Input the character k, judge that there is no such character in the file, and output the last line of character string in the file.
Insert picture description here
Insert picture description here

This is the end of the usage of ReadString to read the contents of a file.

Guess you like

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