C++文件读写操作二 (CFile类)

一、相关函数原型 (详细请参考MSDN)
1、构造函数的一种
CFile(
   LPCTSTR lpszFileName,     //文件名
   UINT nOpenFlags           //文件访问模式
);

2、写入
virtual void Write(
   const void* lpBuf,   //指向用户提供的缓冲区,包含将写入文件中的数据
   UINT nCount   //从缓冲区内传输的字节数。对文本模式的文件,回车换行作为一个字符。
);


3、读取
virtual UINT Read(
   void* lpBuf,         //指向用户提供的缓冲区,包含将读取文件中的数据
   UINT nCount   //文件字节数
);


二、例子:

1、写入文件

 CFile file(_T("test.txt"),CFile::modeCreate|CFile::modeWrite);
 file.Write("hello,world!",strlen("hello,world!"));
 file.Close();

2、读取文件

 CFile file(_T("test.txt"),CFile::modeRead);
 char* pBuf;
 DWORD dwFileLen;
 dwFileLen = file.GetLength();
 pBuf = new char[dwFileLen+1];
 pBuf[dwFileLen] = 0;
 file.Read(pBuf,dwFileLen);
 file.Close();
 MessageBox(CString(pBuf));


 

posted @ 2009-07-30 21:49  trace007  阅读(1749)  评论(3编辑  收藏  举报