MFC,TXT读写,文本流

文本写: 在txt文件中写入主机时间


```cpp
void CUSFDlg::OnBnClickedBtnsave()
{
	CTime t = CTime::GetCurrentTime(); //获取系统日期

	int d = t.GetDay(); //获得几号
	int y = t.GetYear(); //获取年份
	int m = t.GetMonth(); //获取当前月份
	int h = t.GetHour(); //获取当前为几时
	int mm = t.GetMinute(); //获取分钟
	int s = t.GetSecond(); //获取秒
	int w = t.GetDayOfWeek(); //获取星期几,1为星期天,7为星期六
	CString str;
	CString stmp;
	stmp.Format(_T("\n%d"), y);
	str = stmp;
	stmp.Format(_T("%2d"), m);
	str =str+_T("-")+ stmp;
	stmp.Format(_T("%2d"), d);
	str = str + _T("-") + stmp;
	stmp.Format(_T("%2d"), h);
	str = str + _T("  ") + stmp;
	stmp.Format(_T("%2d"), mm);
	str = str + _T(":") + stmp;
	stmp.Format(_T("%2d                       "), s);
	str = str + _T(":") + stmp;

	CString text, strline;
	DWORD m_dwPos = 0;

	CString filePath = _T("D:\\filetest.txt"); //保存文件的路径和文件名
	CStdioFile myFile; //操作文件默认的是Text模式
	CFileException fileException;
	//if (myFile.Open(filePath, CFile::typeText | CFile::modeCreate | CFile::modeReadWrite | CFile::modeNoTruncate), &fileException)
	if (myFile.Open(filePath, CFile::modeReadWrite), &fileException)
	{
		myFile.WriteString(str);


		UpdateData(TRUE);

		myFile.SeekToEnd();
		strline = str;
		myFile.WriteString(strline);
	}
	else
		TRACE("Can't open file %s,error=%u\n", filePath, fileException.m_cause);//异常处理
	myFile.Close(); //文件操作结束关闭

}

`


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值