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

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 第四篇 文件操作的更多相关文章

  1. Html5 学习系列(四)文件操作API

    原文:Html5 学习系列(四)文件操作API 引言 在之前我们操作本地文件都是使用flash.silverlight或者第三方的activeX插件等技术,由于使用了这些技术后就很难进行跨平台.或者跨 ...

  2. Python入门篇-文件操作

    Python入门篇-文件操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.文件IO常用操作 open:打开 read:读取 write:写入 close:关闭 readlin ...

  3. python基础篇(文件操作)

    Python基础篇(文件操作) 一.初始文件操作 使用python来读写文件是非常简单的操作. 我们使用open()函数来打开一个文件, 获取到文件句柄. 然后通过文件句柄就可以进行各种各样的操作了. ...

  4. python语法(四)— 文件操作

    前面几天学习了一写python的基础语法,也学习了分支if,循环while和for.由于之前已经做过几年的开发了,所以我们知道,许多数据来源并不是靠键盘输入到程序中去的,而是通过数据库和文件来获取到的 ...

  5. python基础(四)文件操作和集合

    一.文件操作 对文件的操作分三步: 1.打开文件获取文件的句柄,句柄就理解为这个文件 2.通过文件句柄操作文件 3.关闭文件. 1.文件基本操作: f = open('file.txt','r') # ...

  6. iOS开发——C篇&文件操作

    今天开始C语言中的重点难点就基本上技术忘了,但是还有最后一个知识点不得不提,那就是文件操作. 我们都知道,我们每天都在使用电脑,手机,或者其他电子或者移动设备,其实我们在使用的时候每时每刻都在执行文件 ...

  7. python04篇 文件操作(二)、集合

    一.文件操作(二) 1.1 利用with来打开文件 # with open ,python 会自动关闭文件 with open('a.txt', encoding='utf-8') as f: # f ...

  8. Windows Store App 用户库文件操作

    (1)获取用户库位置 如果想要通过应用程序在用户库中创建文件,首先需要获得用户库中指定的位置,例如图片库.文档库等.这里值得注意的是,在获取用户库的位置之前,必须在Windows应用商店项目的清单文件 ...

  9. 《天书夜读:从汇编语言到windows内核编程》八 文件操作与注册表操作

    1)Windows运用程序的文件与注册表操作进入R0层之后,都有对应的内核函数实现.在windows内核中,无论打开的是文件.注册表或者设备,都需要使用InitializeObjectAttribut ...

随机推荐

  1. CSIC_716_20191111【函数对象、名称空间、作用域、global 和nonlocal】

    函数名是可以被引用,传递的是函数的内存地址.函数名赋值给变量后,只需要在变量后加上括号即可调用函数. 名称空间 内置名称空间:在python解释器中提前定义完的名字 全局名称空间:if.while.f ...

  2. for双重循环中的结构分离(语法结构问题)

    //增加搜索列表 function addSearchList(){ $.get("/mall/h5_get_search_list.html","",func ...

  3. IDEA引入jar但无法导入class

    如图,jar已经导入,但是无法import class 选择setting->maven->Ignored Files,将被忽略的model取消选中就可以解决了

  4. JavaScript中的表单编程

    表单编程 1获取表单相关信息 1.什么是表单元素 1.什么是表单元素 在H TML中表单就是指form标签,它的作用是将用户输入或选择的数据提交给指定的服务器 2.如何获取表单元素 <form ...

  5. VS2010-MFC(常用控件:图片控件Picture Control)

    转自:http://www.jizhuomi.com/software/193.html 本节主要讲一种简单实用的控件,图片控件Picture Control.我们可以在界面某个位置放入图片控件,显示 ...

  6. java-day04

    IntelliJ快捷键 导入包 alt + enter 删除光标所在行 ctrl + y 复制光标所在行 ctrl + d 格式代码 ctrl + alt + l 单行注释 ctrl + / 多行注释 ...

  7. 复杂Linq语句写法

    从网上收藏的复杂Linq语句写法 1.case when: 原型: sql原型: SELECT ProductID, Name, Color, CASE WHEN Color = 'Red' THEN ...

  8. 收藏的链接-Qt

    Qt编写的开源帖子集合(懒人专用) - QTCN开发网 - Powered by phpwind http://www.qtcn.org/bbs/read-htm-tid-85501.html?tds ...

  9. amazeUI表单提交验证--input框required

    效果: html: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  10. lxml etree对存在的xml添加新节点,新节点没有排版格式化

    新添加的时候如果不做处理,是这个样子 要在解析xml加上 parser = etree.XMLParser(remove_blank_text=True)xml = etree.parse(major ...