现在的位置: 首页 > 综合 > 正文

文件操作(CFile)

2013年01月26日 ⁄ 综合 ⁄ 共 940字 ⁄ 字号 评论关闭

//读取文件
void CFileView::OnFilRead()
{
 // TODO: Add your command handler code here
 CFile file;
 TRY
 {
 file.Open("lm.txt",CFile::modeRead);
 }
 CATCH(CFileException , e)
 {
#ifdef _DEBUG
  afxDump<<"File could not be open"<<e->m_cause <<"\n";
#endif

 }
 END_CATCH
 char *pBuf;
 DWORD dwFileLen;
 dwFileLen=file.GetLength ();
 pBuf=new char[dwFileLen+1];
 pBuf[dwFileLen]=0;

 file.Read (pBuf,dwFileLen);
 file.Close ();
 MessageBox(pBuf);
}
//写入文件
void CFileView::OnFilWrite()
{
 // TODO: Add your command handler code here
  CFile file("lm.txt",CFile::modeCreate|CFile::modeWrite);
 file.Write ("WELCOME TO CHINA!",strlen("WELCOME TO CHINA!"));
 CDC *pDC=GetDC();
 pDC->TextOut (0,0,file.GetFileName());
 pDC->TextOut (0,20,file.GetFilePath  ());
 pDC->TextOut (0,40,"文件已经打开");
 file.Close ();
}

 

//C语言中的文件操作

/* FILE *pFile=fopen("1.txt","w");
 fwrite("www.sina.com.cn",1,strlen("www.sina.com.cn"),pFile);
 fseek(pFile,0,SEEK_SET);
 fwrite("ftp:",1,strlen("ftp:"),pFile); 

 MessageBox("OK");

 fflush(pFile);
  fclose(pFile); */

 

抱歉!评论已关闭.