用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,"提示");
}