对文件操作的一些方法

简介: 对文件操作的一些方法

1.获取执行文件的路径


  用函数GetModuleFileName


2.获取执行文件所在目录的方法


  CString   sFile,sPath;      
  //获取主程序所在路径,存在sPath中  
  GetModuleFileName(NULL,sPath.GetBufferSetLength   (MAX_PATH+1),MAX_PATH);  
  sPath.ReleaseBuffer   ();  
          int   nPos;  
  nPos=sPath.ReverseFind   ('//');  
  sPath=sPath.Left   (nPos);  
  sFile   =   sPath   +   //Demo.doc;   //   将被读取的Excel文件名
3.判断文件是否存在的几种方法
   BOOL CPubFunc::FileExist(CString FileName)
 {
   CFileFind fFind;
   return fFind.FindFile(FileName);
}
BOOL CPubFunc::DirectoryExist(CString Path)
{
 WIN32_FIND_DATA fd;
 BOOL ret = FALSE;
    HANDLE hFind = FindFirstFile(Path, &fd);
    if ((hFind != INVALID_HANDLE_VALUE) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
    {
  //目录存在
  ret = TRUE;
    }
    FindClose(hFind);
 return ret;
}
BOOL CPubFunc::CreateDirectory(CString path)
{
 SECURITY_ATTRIBUTES attrib;
 attrib.bInheritHandle = FALSE;
 attrib.lpSecurityDescriptor = NULL;
 attrib.nLength = sizeof(SECURITY_ATTRIBUTES);
 return ::CreateDirectory( path, &attrib);
}


相关文章
|
9月前
|
存储 C语言
文件操作及函数
文件操作及函数
67 0
|
移动开发 .NET SQL
|
XML C# 数据格式
|
分布式数据库 C++ 安全
|
存储 编译器 Windows
【C】文件操作详解
什么是文件 磁盘上的文件是文件。 但是在程序设计中,我们一般谈的文件有两种:程序文件、数据文件
|
存储 C语言 Windows
文件操作(上)
文件操作(上)
61 0
|
程序员 编译器 C语言
文件操作(中)
文件操作(中)
45 0
超详细的文件操作讲解
各位朋友们,大家好啊,今天我要分享的是关于文件操作方面的知识。

热门文章

最新文章