vc 读取txt

void COrderReadFileDlg::OnRead() 
{
	CString strserver,strdatabase,strusr,strpwd,readstring;
	if(file)
	{
	readlen=file.ReadString(readstring);
	if(!readlen)
		{
			GetDlgItem(IDC_BTREAD)->EnableWindow(FALSE);
			return;
		}
	int pos=readstring.Find(" ");
	strserver=readstring.Left(pos);
	readstring=readstring.Right(readstring.GetLength()-pos-1);

	pos=readstring.Find(" ");
	strdatabase=readstring.Left(pos);
	readstring=readstring.Right(readstring.GetLength()-pos-1);

	pos=readstring.Find(" ");
	strusr=readstring.Left(pos);
	readstring=readstring.Right(readstring.GetLength()-pos-1);

	pos=readstring.Find(" ");
	strpwd=readstring.Left(pos);
	readstring=readstring.Right(readstring.GetLength()-pos-1);
	
	GetDlgItem(IDC_EDSERVER)->SetWindowText(strserver);
	GetDlgItem(IDC_EDDATABASE)->SetWindowText(strdatabase);
	GetDlgItem(IDC_EDUSR)->SetWindowText(strusr);
	GetDlgItem(IDC_EDPWD)->SetWindowText(strpwd);
	}

}

void COrderReadFileDlg::OnOpen() 
{
	try{
	file.Open("test1.txt",CFile::modeRead);
	GetDlgItem(IDC_BTOPEN)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTREAD)->EnableWindow(TRUE);
	AfxMessageBox("文件已打开");
	}catch(CFileException *e)
	{
		TCHAR szBuf[256]; 
		e->GetErrorMessage(szBuf,256,NULL);
		MessageBox(szBuf,_T("Warning"));
		e->Delete();
	}

}

void COrderReadFileDlg::OnExit() 
{
	file.Close();
	this->OnCancel();	
}
1.创建CStdioFile对象file
2.file.Open
2.file..ReadString读取一行
3.file.Close
 example151

void CLogFileDlg::OnWrite() 
{
	CTime time;
	time=CTime::GetCurrentTime();
	FILE* fp;
	fp=fopen("test.log","a");
	fprintf(fp,"%s-------\n",time.Format("%d-%H-%M-%S"));
	fclose(fp);

}

void CLogFileDlg::OnRead() 
{
	CString tmp,str;
	CStdioFile file;
	try{
	int i=file.Open("test.log",CFile::modeRead);
	if(i==0)
		return;
	}catch(CFileException *e)
	{
		TCHAR szBuf[256]; 
		e->GetErrorMessage(szBuf,256,NULL);
		MessageBox(szBuf,_T("Warning"));
		e->Delete();
	}
	while(1)
	{
	DWORD i=file.ReadString(str);
	if(i==0)goto end;
	tmp+=str;
	tmp+="\r\n";
	}
	end:
	m_edlog.SetWindowText(tmp);
}

1.创建CStdioFile对象file,.file.Open,file..ReadString读取一行
2.FILE* fp;fp=fopen("test.log","a");fprintf(fp,"%s-------\n",time.Format("%d-%H-%M-%S"));写文件
example152

你可能感兴趣的:(vc 读取txt)