Windows API 第四篇 文件操作
创建或打开文件(也可用于打开管道,油槽,硬件设备等):
HANDLE CreateFile(
LPCTSTR lpFileName, // file name
DWORD dwDesiredAccess, // access mode
DWORD dwShareMode, // share mode
LPSECURITY_ATTRIBUTES lpSecurityAttributes, // SD
DWORD dwCreationDisposition, // how to create
DWORD dwFlagsAndAttributes, // file attributes
HANDLE hTemplateFile // handle to template file
);
参数说明:
lpFileName:文件名
dwDesiredAccess:对文件的打开模式,取值有:GENERIC_READ或者GENERIC_WRITE或者 0
dwShareMode: 文件的共享模式,FILE_SHARE_READ FILE_SHARE_WRITE FILE_SHARE_DELETE
lpSecurityAttributes: 指向 SECURITY_ATTRIBUTES结构的安全属性,可指定返回句柄是否可被继承
dwCreationDisposition: 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. |
dwFlagsAndAttributes:文件属性取值有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 hFile, // handle to file
LPVOID lpBuffer, // data buffer
DWORD nNumberOfBytesToRead, // number of bytes to read
LPDWORD lpNumberOfBytesRead, // number of bytes read
LPOVERLAPPED lpOverlapped // overlapped buffer
); 返回值:失败返回FALSE,成功赶回TRUE; 写文件:
BOOL WriteFile(
HANDLE hFile, // handle to file
LPCVOID lpBuffer, // data buffer
DWORD nNumberOfBytesToWrite, // number of bytes to write
LPDWORD lpNumberOfBytesWritten, // number of bytes written
LPOVERLAPPED 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 lpExistingFileName, // name of an existing file
LPCTSTR lpNewFileName, // name of new file
BOOL bFailIfExists // operation if file exists
);
返回值:成功返回非0,失败返回0 删除文件:
BOOL DeleteFile(
LPCTSTR lpFileName // 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 hFile, // handle to file
LPDWORD lpFileSizeHigh // high-order word of file size
);
返回文件低32位,高32位为第二个参数,第二个参数一般为NULL 获取文件名:
short GetFileTitle(
LPCTSTR lpszFile, // path and file name
LPTSTR lpszTitle, // file name buffer
WORD cbBuf // 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
Windows API 第四篇 文件操作的更多相关文章
- Html5 学习系列(四)文件操作API
原文:Html5 学习系列(四)文件操作API 引言 在之前我们操作本地文件都是使用flash.silverlight或者第三方的activeX插件等技术,由于使用了这些技术后就很难进行跨平台.或者跨 ...
- Python入门篇-文件操作
Python入门篇-文件操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.文件IO常用操作 open:打开 read:读取 write:写入 close:关闭 readlin ...
- python基础篇(文件操作)
Python基础篇(文件操作) 一.初始文件操作 使用python来读写文件是非常简单的操作. 我们使用open()函数来打开一个文件, 获取到文件句柄. 然后通过文件句柄就可以进行各种各样的操作了. ...
- python语法(四)— 文件操作
前面几天学习了一写python的基础语法,也学习了分支if,循环while和for.由于之前已经做过几年的开发了,所以我们知道,许多数据来源并不是靠键盘输入到程序中去的,而是通过数据库和文件来获取到的 ...
- python基础(四)文件操作和集合
一.文件操作 对文件的操作分三步: 1.打开文件获取文件的句柄,句柄就理解为这个文件 2.通过文件句柄操作文件 3.关闭文件. 1.文件基本操作: f = open('file.txt','r') # ...
- iOS开发——C篇&文件操作
今天开始C语言中的重点难点就基本上技术忘了,但是还有最后一个知识点不得不提,那就是文件操作. 我们都知道,我们每天都在使用电脑,手机,或者其他电子或者移动设备,其实我们在使用的时候每时每刻都在执行文件 ...
- python04篇 文件操作(二)、集合
一.文件操作(二) 1.1 利用with来打开文件 # with open ,python 会自动关闭文件 with open('a.txt', encoding='utf-8') as f: # f ...
- Windows Store App 用户库文件操作
(1)获取用户库位置 如果想要通过应用程序在用户库中创建文件,首先需要获得用户库中指定的位置,例如图片库.文档库等.这里值得注意的是,在获取用户库的位置之前,必须在Windows应用商店项目的清单文件 ...
- 《天书夜读:从汇编语言到windows内核编程》八 文件操作与注册表操作
1)Windows运用程序的文件与注册表操作进入R0层之后,都有对应的内核函数实现.在windows内核中,无论打开的是文件.注册表或者设备,都需要使用InitializeObjectAttribut ...
随机推荐
- SpringBatch批处理框架:入门项目
1.项目结构如下:
- jdk配置到tomcat配置
1.jdk配置 首先下载jdk 这里我下载的是 jdk1.8 (jdk1.8.0_181\) 然后,安装 我的安装路径 C:\Program Files\Java\jdk1.8.0_181\ 我选择默 ...
- 树形dp——cf1010D
一个点的改变如果对根节点的值不会造成任何影响,那么这个点的所有子节点的改变也不会对根节点造成影响 因为每次只改一个叶子节点,也就是一条到根的路径,可以先预处理出初始情况下的每个结点的值 分别讨论根节点 ...
- 样本方差的抽样分布 χ2(n) 卡方分布_样本方差 卡方分布
样本方差的抽样分布 χ2(n) 卡方分布_样本方差 卡方分布 样本方差的抽样分布 χ2(n) 卡方分布 t分布.卡方分布.f分布均要求总体服从正态分布. 若n个相互独立的随机变量ξ1,ξ2,-,ξn ...
- pycharm同时使用python2.7和python3.5设置方法
pycharm同时使用python2.7和python3.5设置方法 - CSDN博客https://blog.csdn.net/qwerty200696/article/details/530159 ...
- 用原生js封装轮播图
原生js封装轮播图 对于初学js的同学来说,轮播图还是一个难点,尤其是原生js封装轮播图代码,下面是我之前做的一个轮播图项目中封装好的一些代码,有需要的同学可以看一下,有什么不懂的可以看注释,注释看不 ...
- 4_3.springboot2.x之默认访问首页和国际化
1.默认访问首页 1.引入thymeleaf和引入bootstrap <!--引入thymeleaf--> <dependency> <groupId>org.sp ...
- LINUX交换分区
交换分区最大容量为64G,最多只能建32个, 创建交换分区 #fdisk /dev/hdaànà+容量àpàt(修改系统ID)à分区号à82àpàw #mkswap /dev/hda ...
- 迭代器/生成器/装饰器 /Json & pickle 数据序列化
本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 1.列表生成式,迭代器&生成器 列表生成式 孩子,我现在有个需 ...
- Nginx反向代理Odoo并转为https
生成证书文件 生成自签名证书,并放在指定位置 $ openssl req -x509 -days 3650 -subj '/CN=odoo.youyun.com/' -nodes -newkey rs ...