您的位置:首页 > 其它

关于获取文件路径,查找文件是否存在,创建文件的一些函数

2011-12-08 09:46 435 查看
1、GetModuleFileName

函数原型:

  DWORD GetModuleFileName(

  HMODULE hModule,

  LPTSTR lpFilename,

  DWORD nSize

  );

  函数参数说明:

  hModule HMODULE 装载一个程序实例的句柄。如果该参数为NULL,该函数返回该当前应用程序全路径。

  lpFileName LPTSTR 是你存放返回的名字的内存块的指针,是一个输出参数

  nSize DWORD ,装载到缓冲区lpFileName的最大值

  函数返回值:

  如果返回为成功,将在lpFileName的缓冲区当中返回相应模块的路径,如果所为的nSize过小,哪么返回仅按所设置缓冲区大小返回相应字符串内容。

  如果函数失败,返回值将为0,并返回GetLastError异常代码。

  需要的头文件为:

  include Windows.h

2、CString::ReverseFind

ReverseFind在一个较大的字符串中从末端开始查找某个字符
  CString::ReverseFind

  int ReverseFind( TCHAR ch ) const;

  返回值:

  返回此CString对象中与要求的字符匹配的最后一个字符的索引;如果没有找到需要的字符则返回-1。

  参数:
ch要搜索的字符。
  说明:

  此成员函数在此CString对象中搜索与一个子串匹配的最后一个字符。此函数类似于运行时函数strrchr。

CString的一个函数ReverseFind,从名字上可以看出是从尾部,即右边开始寻找,可是字符串的字母编号确还是从左边算的,而且从0开始

例如CString aa("abcdef");

aa.ReverseFind('e') == 4

3、GetFileAttributes Function

  为一个指定的文件或目录返回文件系统的属性。可以使用GetFileAttributesEx 函数获得更多的属性信息。如果要实现交互式操作,可以使用GetFileAttributesTransacted 函数。

  DWORD WINAPI GetFileAttributes(

  __in LPCTSTR lpFileName

  );

  参数

  lpFileName [in]

  文件或目录的名字,对于ANSI版本,名字不能大于MAX_PATH。

  返回值

  如果函数成功,返回值包含文件或目录的属性。如果函数失败,返回值是INVALID_FILE_ATTRIBUTES。

  备注

  当该函数作用在一个挂载文件夹时,它返回目录的文件系统的属性,而不是根目录的信息。为了获得与文件属性关联的卷信息,可以调用GetVolumeNameForVolumeMountPoint 函数。

4、CreateDirectory,

This function creates a new directory. If the underlying file system supports security on files and directories, the function applies a specified security descriptor to the new directory.

  A remote application interface (RAPI) version of this function exists, and it is namedCeCreateDirectory.

  BOOL CreateDirectory(LPCTSTR lpPathName, LPSECURITY_ATTRIBUTESlpSecurityAttributes
);

  ParameterslpPathName [in] Long pointer to a null-terminated string that specifies the path of the directory to be created. There is a default string size limit for paths of MAX_PATH characters. This limit is related to how theCreateDirectory
function parses paths.

  lpSecurityAttributes [in] Ignored; set to NULL. Return ValuesNonzero indicates success. Zero indicates failure. To get extended error information, callGetLastError.

  RemarksSome file systems, such as NTFS, support compression or encryption for individual files and directories. On volumes formatted for such a file system, a new directory inherits the compression and encryption attributes of its parent directory.

  第一个参数值为文件夹名称,第二个参数值为安全属性,一般设置为NULL即可。如果正确创建,返回值为1,如果没有正常创建文件夹,则返回0。

  特别的:该函数每次调用时都只能创建一级文件夹,即文件夹中不能再包含子文件夹。

  当希望创建含有子文件夹的文件夹时,可以先使用该函数创建一级文件夹,然后再使用该函数在一级文件夹下创建子文件夹。如:

  希望创建:d:\\TEST\\temp,

  则:CString str = “d:\\TEST”;

  CreateDirectory(str, NULL);

  str = str + \\temp;

  CreateDirectory(str, NULL);

5、copyfile

函数原型

  BOOL CopyFile(LPCTSTR lpExistingFileName,LPCTSTR lpNewFileName,BOOL bFailIfExists );

说明

  复制文件,与vb的filecopy命令相似

返回值

  Long,非零表示成功,零表示失败。会设置GetLastError

参数表

  参数类型及说明 :

  lpExistingFileName String,源文件名

  lpNewFileName String,目标文件名

  bFailIfExists Long,如果设为TRUE(非零),那么一旦目标文件已经存在,则函数调用会失败。否则目标文件被改写
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐