C/C++ 文件操作之CreateFile函数、ReadFile函数和WriteFile函数的用法
1.CreateFile最初是专门为文件交互而开发的,但后来被扩展和增强,以包括Windows开发人员可用的大多数其他类型的I / O设备和机制
HANDLE WINAPI CreateFile( _In_ LPCTSTR lpFileName, _In_ DWORD dwDesiredAccess, _In_ DWORD dwShareMode, _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes, _In_ DWORD dwCreationDisposition, _In_ DWORD dwFlagsAndAttributes, _In_opt_ HANDLE hTemplateFile );
#include <stdio.h> #include <windows.h> int main() { HANDLE hFILE=CreateFile("1.txt",GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if(hFILE==INVALID_HANDLE_VALUE) { printf("CreateFile error\n"); return 0; } if(SetFilePointer(hFILE,0,NULL,FILE_END)==-1) { printf("SetFilePointer error\n"); return 0; } char buff[256]="hello"; DWORD dwWrite; if(!WriteFile(hFILE,&buff,strlen(buff),&dwWrite,NULL)) { printf("WriteFile error\n"); return 0; } printf("write %d.\n",dwWrite); printf("done.\n"); CloseHandle(hFILE); return 0; }
2. ReadFile函数 从文件指针指向的位置开始将数据读出到一个文件中, 且支持同步和异步操作,如果文件打开方式没有指明FILE_FLAG_OVERLAPPED的话,当程序调用成功时,它将实际读出文件的字节数保存到lpNumberOfBytesRead指明的地址空间中。FILE_FLAG_OVERLAPPED 允许对文件进行重叠操作。
BOOL WINAPI ReadFile( __in HANDLE hFile, // 文件句柄 __out LPVOID lpBuffer, // 接收数据用的 buffer __in DWORD nNumberOfBytesToRead, // 要读取的字节数 __out LPDWORD lpNumberOfBytesRead, // 实际读取到的字节数 __in LPOVERLAPPED lpOverlapped // OVERLAPPED 结构,一般设定为 NULL );
BOOL Read(char *filePath) { HANDLE pFile; DWORD fileSize; char *buffer,*tmpBuf; DWORD dwBytesRead,dwBytesToRead,tmpLen; pFile = CreateFile(filePath,GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, //打开已存在的文件 FILE_ATTRIBUTE_NORMAL, NULL); if ( pFile == INVALID_HANDLE_VALUE) { printf("open file error!\n"); CloseHandle(pFile); return FALSE; } fileSize = GetFileSize(pFile,NULL); //得到文件的大小 buffer = (char *) malloc(fileSize); ZeroMemory(buffer,fileSize); dwBytesToRead = fileSize; dwBytesRead = 0; tmpBuf = buffer; do{ //循环读文件,确保读出完整的文件 ReadFile(pFile,tmpBuf,dwBytesToRead,&dwBytesRead,NULL); if (dwBytesRead == 0) break; dwBytesToRead -= dwBytesRead; tmpBuf += dwBytesRead; } while (dwBytesToRead > 0); // TODO 处理读到的数据 buffer free(buffer); CloseHandle(pFile); return TRUE; }
3.WriteFile函数
BOOL WINAPI WriteFile( __in HANDLE hFile, // 文件句柄 __in LPCVOID lpBuffer, // 要写入的数据 __in DWORD nNumberOfBytesToWrite, // 要写入的字节数 __out LPDWORD lpNumberOfBytesWritten, // 实际写入的字节数 __in LPOVERLAPPED lpOverlapped // OVERLAPPED 结构,一般设定为 NULL );
BOOL Write(char *buffer, DWORD contentLen) { HANDLE pFile; char *tmpBuf; DWORD dwBytesWrite,dwBytesToWrite; pFile = CreateFile(filePath,GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, //总是创建文件 FILE_ATTRIBUTE_NORMAL, NULL); if ( pFile == INVALID_HANDLE_VALUE) { printf("create file error!\n"); CloseHandle(pFile); return FALSE; } dwBytesToWrite = contentLen; dwBytesWrite = 0; tmpBuf = buffer; do{ //循环写文件,确保完整的文件被写入 WriteFile(pFile,tmpBuf,dwBytesToWrite,&dwBytesWrite,NULL); dwBytesToWrite -= dwBytesWrite; tmpBuf += dwBytesWrite; } while (dwBytesToWrite > 0); CloseHandle(pFile); return TRUE; } 链接:https://www.365jz.com/article/24618 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
将数据写入一个文件。该函数比fwrite函数要灵活的多。也可将这个函数应用于对通信设备、管道、套接字以及邮槽的处理。返回时,TRUE(非零)表示成功,否则返回零。会设置GetLastError
链接:https://www.365jz.com/article/24618
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
链接:https://www.365jz.com/article/24618
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。