开发环境里没有定义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; }