CFile类对文件的操作

用vc++定义四个按钮,然后添加单击事件如下,参数有不懂的,可以去看看帮助文档



void CDemo11Dlg::OnButton1() 

{
FILE *pFile=fopen("demo.txt","w+t");//以读写形式打开文件
if(pFile)  //判断文件是否被打开
{
char *pchData="hello world"; //设置待写入的数据
fwrite(pchData,sizeof(char),strlen(pchData),pFile);//向文件中写入数据
//393页
fclose(pFile);//关闭文件

}
}


void CDemo11Dlg::OnButton2() 
{
FILE *pFile=fopen("demo.txt","r");
if(pFile)
{
char pchData[MAX_PATH]={0};
fread(pchData,sizeof(char),MAX_PATH,pFile);
fclose(pFile);
MessageBox(pchData,"提示");
}
}


void CDemo11Dlg::OnButton3() 
{
CFile file;//定义一个文件对象
file.Open("demo.txt",CFile::modeCreate|CFile::modeReadWrite);//创建文件
file.Write("复兴之路,大国崛起",18);//写入数据
file.Close();//关闭文件





}
#include <typeinfo.h>
void CDemo11Dlg::OnButton4() 
{
// TODO: Add your control notification handler code here
CFile file; //定义文件对象
file.Open("demo.txt",CFile::modeRead); //以读写的方式打开文件
unsigned char pchData[MAX_PATH]={0}; //定义数据缓冲区
file.Read(pchData,MAX_PATH); //读取数据到缓冲区
CFileStatus flStatus; //定义文件状态
file.GetStatus(flStatus); //获取文件状态
file.Close(); //关闭文件


//获取文件创建时间
CString createtime=flStatus.m_ctime.Format("%Y-%m-%d %H:%M:%S");
CString hint ="文件内容";     //定义一个字串


hint+=(char *)pchData; //添加字符串
hint+="\n"; //添加换行符
hint+="创建时间"; //添加数据
hint+=createtime;
if (typeid(hint) == typeid(char))
{
AfxMessageBox("我是字符串");
}
MessageBox(hint,"提示");


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值