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));
}
Result:
Example 1
input character +, which exists in the file, and output this line of character string.
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.