CFile 类 CFile m_tmpFile;
CString path="D:\\test\\test.txt";
LRESULT res;
打开文件:res = m_tmpFIle.Open(path, CFile::modeRead);
备注:打开一个文件的时候,即使是以CFile::modeCreat方式打开(文件不存在的时候,创建文件),文件所在的目录也得必须存在,否则打开文件失败
当res=1,打开成功,res=0, 打开失败
CString m_iniName = m_tmpFIle.GetFileName; //获得文件的完整路径名D:\\test\\test.txt
int pos=m_iniName.ReverseFind('.');
if(pos > 0)
{
m_iniName=m_iniName.Left(pos);
}
此时,m_iniName=“D:\\test\\test”
文件指针的移动:
m_tmpFile.Seek(-100, CFile::End); 从文件末尾向前移动100个字节
m_tmpFIle.Seek(50, CFile::Begin); 从文件开头向后移动50个字节
m_tmpFile.Seek(20, CFile::Current); 从文件当前位置向后移动20个字节
m_tmpFile.SeekToEnd(); 移动到文件末尾
m_tmpFIle.SeekToBegin(); 移动到文件开头
读写文件:
CString read="你好,hello";
int length= strlen(read);
m_tmpFile.Write(read, length);
char buf[100];
m_tmpFile.Read(buf, 100);
文件关闭:
m_tmpFile.Close();
创建目录:
CString destDir = "D:\\hello\\test";
CreateDirectory(destDir,NULL);
CreateDirectory创建目录,只能创建一级目录,不能创建子文件夹。即要创建destDir,必须hello目录是存在的,才能成功创建下面的test文件。否则会创建失败
要创建多级目录的话,可以使用系统命令,利用dos命令来创建,格式为 system("md D:\\hello\\test"); 可以创建多级目录
文件的移动:
CString sorce;
CString dest;
MoveFile(sorce, dest); //其中,sorce为要移动的源文件的绝对路径名,dest 为移动后的路径和名字(移动文件后支持更改名字)
CString 相关操作:
CString tmp;
字符的替换:tmp.Replace("\/", "\\"); //将字符串中的/替换成\
另外,推荐一篇比较全面的文件操作的文章http://blog.csdn.net/zhuang_rui/article/details/1804875