写文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <stdio.h> #include <windows.h> void main(int argc, char *argv[]) { HANDLE hFile; char szBuffer [256] = {"SysProgram"}; char szFileName [128] = "C:\\1.txt" ; DWORD dw = 0; hFile = CreateFile (szFileName, GENERIC_READ |GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { printf("Create file error \n"); return; } WriteFile(hFile , &szBuffer , strlen(szBuffer), & dw, NULL); //写文件 CloseHandle(hFile); //关闭句柄 printf("%s \n" , szBuffer); return; } |
读文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <stdio.h> #include <windows.h> void main(int argc, char *argv[]) { HANDLE hFile; char szBuffer[256] = {0}; char szFileName[128] = "C:\\1.txt"; DWORD dw = 0; hFile = CreateFile (szFileName, GENERIC_READ, FILE_SHARE_READ, NULL , OPEN_EXISTING , FILE_ATTRIBUTE_NORMAL, NULL); ReadFile (hFile, szBuffer, sizeof(szBuffer), &dw, NULL); printf("%s \n", szBuffer); return; } |
转载请注明:exchen's blog » Windows API 函数读写文件