码迷,mamicode.com
首页 > 其他好文 > 详细

CFileDialog 、CFile 如何进行文件操作 [转]

时间:2014-06-21 00:07:01      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   http   ext   

如何进行文件操作

[1]显示对话框,取得文件名

1 CString FilePathName;
2 CFileDialog dlg(TRUE);//TRUE为OPEN对话框,FALSE为SAVE AS对话框
3 if (dlg.DoModal() == IDOK)
4 FilePathName=dlg.GetPathName();

相关信息:CFileDialog 用于取文件名的几个成员函数:
假如选择的文件是C:\WINDOWS\TEST.EXE

(1) GetPathName();   取文件名全称,包括完整路径。取回C:\WINDOWS\TEST.EXE
(2) GetFileTitle();      取文件全名:TEST.EXE
(3) GetFileName();   取回TEST
(4) GetFileExt();   取扩展名EXE

 

[2]打开文件
CFile file("C:\HELLO.TXT",CFile::modeRead);//只读方式打开
//CFile::modeRead可改为 CFile::modeWrite(只写),
//CFile::modeReadWrite(读写),CFile::modeCreate(新建)
例子:
{
CFile file;
file.Open("C:\HELLO.TXT",CFile::modeCreate|Cfile::modeWrite);
.
.
.
}

 

[3]移动文件指针
file.Seek(100,CFile::begin);///从文件头开始往下移动100字节
file.Seek(-50,CFile::end);///从文件末尾往上移动50字节
file.Seek(-30,CFile::current);///从当前位置往上移动30字节
file.SeekToBegin();///移到文件头
file.SeekToEnd();///移到文件尾

 

[4]读写文件
读文件:
char buffer[1000];
file.Read(buffer,1000);
写文件:
CString string("自强不息");
file.Write(string,8);

 

[5]关闭文件
file.Close();

 

转自:http://www.cnblogs.com/lidabo/p/3470085.html

CFileDialog 、CFile 如何进行文件操作 [转],布布扣,bubuko.com

CFileDialog 、CFile 如何进行文件操作 [转]

标签:style   class   blog   code   http   ext   

原文地址:http://www.cnblogs.com/vranger/p/3795663.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!