CreateFile WriteFile ReadFile FlushFileBuffers的基本用法
2010-11-09 18:07
393 查看
开发环境里没有定义UNICODE
#include <Windows.h> int _tmain(int argc, _TCHAR* argv[]) { HANDLE hFile; hFile = CreateFile("E://Test.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { printf("Invalid handle when read file create/n"); return -1; } char buffer[256] = {0}; DWORD readBytes = 0; BOOL flag = ReadFile(hFile, buffer, 256, &readBytes, NULL); if (flag == FALSE) { printf("ReadFile failed/n"); return -1; } printf("Read bytes = %d/n", readBytes); printf("Read content = %s/n", buffer); CloseHandle(hFile); printf("****************************/n/n"); hFile = CreateFile("E://Test.txt", GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { printf("Invalid handle/n"); return -1; } char outBuffer[256] = "/r/nwhat a fucking day!"; long distance = 0; DWORD retVal = 0; retVal = SetFilePointer(hFile, 0, NULL, FILE_END); if (retVal == 0xFFFFFFFF) { retVal = GetLastError(); printf("LastError = %d/n", retVal); CloseHandle(hFile); return -1; } DWORD outBytes = 0; flag = WriteFile(hFile, outBuffer, strlen(outBuffer), &outBytes, NULL); if (flag == FALSE) { printf("Write file failed/n"); return -1; } if (0 == FlushFileBuffers(hFile)) //如果WriteFile执行成功,这里就不需要调用 { printf("Flush failed/n"); } retVal = SetFilePointer(hFile, 0, NULL, FILE_BEGIN); if (retVal == 0xFFFFFFFF) { retVal = GetLastError(); printf("LastError = %d/n", retVal); CloseHandle(hFile); return -1; } char readBuffer[256] = {0}; flag = ReadFile(hFile, readBuffer, 256, &readBytes, NULL); if (flag == FALSE) { printf("ReadFile failed/n"); return -1; } printf("readBuffer = %s/n", readBuffer); CloseHandle(hFile); return 0; }
相关文章推荐
- Create, Read, Write, Copy, Move and Delete a Text File using C# and VB.NET
- CreateFile ReadFile WriteFile 详解
- Create, Read, Write, Copy, Move and Delete a Text File using C#
- open,read,write的基本用法
- CreateFile ReadFile WriteFile读写文件操作
- RandomAccessFile,read,write,复制文件,指针,批量读写,基本类型读写
- CreateFile ReadFile WriteFile 详解
- <Win32 API> 文件操作CreateFile/ReadFile/WriteFile
- Create, Read, Write, Copy, Move and Delete a Text File using C#
- Windows API一日一练(55)FlushFileBuffers和SetFilePointer函数
- Azure File Storage 基本用法 -- Azure Storage 之 File
- Windows API介绍及用法:CreateFileMapping和MapViewOfFile函数(进程间数据共享)
- epoll_create epoll_ctl epoll_wait close epoll和select的简单比较 (read和write或send和recv当返回值<0且errno=EAGAIN表示
- Win32 WriteFile and ReadFile
- Write/Read XML File
- Liunx FS 高层函数 Create/Open/Close/Read/Write
- Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or rem
- write,read用法,笔记。
- Write operations are not allowed in read-only mode (FlushMode.NEVER)
- hadoop序列文件读与写 (SequenceWriteReadFile)