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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值