博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Windows API 第四篇 文件操作
阅读量:6241 次
发布时间:2019-06-22

本文共 3883 字,大约阅读时间需要 12 分钟。

创建或打开文件(也可用于打开管道,油槽,硬件设备等):

HANDLE CreateFile(  LPCTSTR ,                         // file name  DWORD ,                      // access mode  DWORD ,            // share mode  LPSECURITY_ATTRIBUTES , // SD  DWORD ,                // how to create  DWORD ,                 // file attributes  HANDLE           // handle to template file); 参数说明: :文件名 :对文件的打开模式,取值有:GENERIC_READ或者GENERIC_WRITE或者 0 : 文件的共享模式,FILE_SHARE_READ   FILE_SHARE_WRITE   FILE_SHARE_DELETE  : 指向 SECURITY_ATTRIBUTES结构的安全属性,可指定返回句柄是否可被继承  : Specifies which action to take on files that exist, and which action to take when files do not exist. For more information about this parameter, see the Remarks section. This parameter must be one of the following values.
CREATE_NEW Creates a new file. The function fails if the specified file already exists.
CREATE_ALWAYS Creates a new file. If the file exists, the function overwrites the file, clears the existing attributes, and combines the file attributes and flags specified by dwFlagsAndAttributes with FILE_ATTRIBUTE_ARCHIVE.
OPEN_EXISTING Opens the file. The function fails if the file does not exist.

For a discussion of why you should use the OPEN_EXISTING flag if you are using the CreateFile function for devices, see Remarks.

OPEN_ALWAYS Opens the file, if it exists. If the file does not exist, the function creates the file as if dwCreationDisposition were CREATE_NEW.
TRUNCATE_EXISTING Opens the file. Once opened, the file is truncated so that its size is zero bytes. The calling process must open the file with at least GENERIC_WRITE access. The function fails if the file does not exist.

  

:文件属性取值有FILE_ATTRIBUTE_ARCHIVE,FILE_ATTRIBUTE_ENCRYPTED,FILE_ATTRIBUTE_HIDDEN FILE_ATTRIBUTE_NORMAL,FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,FILE_ATTRIBUTE_OFFLINE,FILE_ATTRIBUTE_READONLY FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_TEMPORARY 返回值:成功返回句柄,失败返回INVALID_HANDLE_VALUE 读文件: BOOL ReadFile(  HANDLE ,                // handle to file  LPVOID ,             // data buffer  DWORD ,  // number of bytes to read  LPDWORD , // number of bytes read  LPOVERLAPPED     // overlapped buffer); 返回值:失败返回FALSE,成功赶回TRUE; 写文件: BOOL WriteFile(  HANDLE ,                    // handle to file  LPCVOID ,                // data buffer  DWORD ,     // number of bytes to write  LPDWORD ,  // number of bytes written  LPOVERLAPPED         // overlapped buffer); 返回值:

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

 

复制文件:

BOOL CopyFile( LPCTSTR , // name of an existing file 

                            LPCTSTR , // name of new file 

                            BOOL // operation if file exists );

返回值:成功返回非0,失败返回0 删除文件: BOOL DeleteFile(  LPCTSTR    // file name); 只需提供文件名即可,成功返回非0,失败返回0 判断某一文件或目录是否存在: BOOL PathFileExists(    LPCTSTR pszPath    );
pszPath
[in] Pointer to a null-terminated string of maximum length MAX_PATH that contains the full path of the object to verify.

返回值:

Returns TRUE if the file exists, or FALSE otherwise. Call GetLastError for extended error information.

 

 

获取文件大小:

DWORD GetFileSize(  HANDLE ,           // handle to file  LPDWORD   // high-order word of file size); 返回文件低32位,高32位为第二个参数,第二个参数一般为NULL 获取文件名: short GetFileTitle(  LPCTSTR ,  // path and file name  LPTSTR ,  // file name buffer  WORD          // length of buffer);

Parameters

lpszFile
[in] Pointer to the name and location of a file.
lpszTitle
[out] Pointer to a buffer that receives the name of the file.
cbBuf
[in] Specifies the length, in
TCHARs, of the buffer pointed to by the
lpszTitle parameter. For the ANSI version of the function, this is in bytes; for the Unicode version, this is in characters.

Return Values

If the function succeeds, the return value is zero.

If the file name is invalid, the return value is unknown. If there is an error, the return value is a negative number

转载于:https://www.cnblogs.com/priarieNew/p/9754066.html

你可能感兴趣的文章
Testng(二):监听
查看>>
重构改善既有的代码设计(代码的坏味道)
查看>>
入门量子计算
查看>>
为什么全栈JavaScript经常被黑,而Java却不会被黑?
查看>>
Java设计模式的6大原则
查看>>
在2018年如何优雅的开发一个typescript语言的npm包?
查看>>
一些小小的总结
查看>>
Homestead 环境搭建
查看>>
Retrofit源码分析
查看>>
零基础兴趣或者转行学习Python,我们应该如何入门呢?
查看>>
electron 仿制QQ登录界面
查看>>
基于Spring Boot实现图片上传/加水印一把梭操作
查看>>
聊聊storm的LoggingClusterMetricsConsumer
查看>>
vue-waterfall2 基于Vue.js 瀑布流组件
查看>>
Vue一个案例引发的递归组件的使用
查看>>
Canvas 实现一个简单的贪吃蛇
查看>>
前后端导出/下载excel方法
查看>>
JVM之垃圾回收与内存动态分配
查看>>
Numpy中的广播原则/机制
查看>>
DOM编程系列之事件
查看>>