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 ...
随机推荐
- ipsec原理(转载)
IPSec VPN是目前VPN技术中点击率非常高的一种技术,同时提供VPN和信息加密两项技术,这一期专栏就来介绍一下IPSec VPN的原理.IPSec VPN应用场景 IPSec VPN的应用场景分 ...
- MFC打开/保存文件对话框:CFileDialog
MFC打开/保存文件对话框:CFileDialog CFileDialog 文件选择对话框的使用:首先构造一个对象并提供相应的参数,构造函数原型如下: CFileDialog::CFileDial ...
- c# 中Linq Lambda 的ToLookup方法的使用
同样直接上代码: List<Student> ss = new List<Student>(); Student ss1 = , Age = , Name = " } ...
- vue使用axios实现前后端通信
安装依赖 npm install --save axios # vue-axios是对axios的简单封装 npm install --save vue-axios 用例 在main.js里面进行全局 ...
- BCD Code ZOJ - 3494 AC自动机+数位DP
题意: 问A到B之间的所有整数,转换成BCD Code后, 有多少个不包含属于给定病毒串集合的子串,A,B <=10^200,病毒串总长度<= 2000. BCD码这个在数字电路课上讲了, ...
- SQL Serve 临时表
SQL Server 支持临时表.临时表就是那些名称以井号 (#) 开头的表.如果当用户断开连接时没有除去临时表,SQL Server 将自动除去临时表.临时表不存储在当前数据库内,而是存储在系统数据 ...
- C 遍历目录及其子目录
遍历某一目录,获取该目录下所有文件路径的数组 #include <iostream> #include <dirent.h> #include <vector> v ...
- Java开发系列-MySQL
概述 数据库 公司 特点 Mysql Oracle 开源的数据库 社区版免费 商业版是收费的 Oracle Oracle 大型的 收费的数据库 DB2 IBM 大型的 收费的数据库 一般用于银行系统 ...
- 移植 Busybox
下载 busybox 从 http://www.busybox.net/downloads/busybox1.1.3.tar.gz/下载 busybox1.1.3 到/tmp 目录当中,并解压. ...
- LINUX挂接Windows文件共享
Windows网络共享的核心是SMB/CIFS,在linux下要挂接(mount)windows的磁盘共享,就必须安装和使用samba 软件包.现在流行的linux发行版绝大多数已经包含了samba软 ...