#include "stdafx.h"
#include<windows.h>
#include<stdio.h>
HANDLE hFile;
void* fun(void*){
char hw[]="helloworld";
DWORD dwWriten;
if(!WriteFile(hFile,(LPVOID)hw,sizeof(hw),&dwWriten,NULL)){//ERROR_ACCESS_DENIED(5)
printf("WriteFile failed:%d\n",GetLastError());
}
if(!FlushFileBuffers(hFile)){
printf("Flush failed\n");
}
return NULL;
}
int _tmain(int argc, _TCHAR* argv[])
{
hFile=CreateFile("d:\\my.txt",
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_WRITE|FILE_SHARE_READ,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(INVALID_HANDLE_VALUE==hFile){
printf("CreateFile failed:%d\n",GetLastError());
return 1;
}
HANDLE hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)fun,NULL,0,NULL);
if(NULL==hThread){
printf("CreateThread failed:%d\n",GetLastError());
return 1;
}
WaitForSingleObject(hThread,INFINITE);
SetFilePointer(hFile, 0, 0, FILE_BEGIN);
char buf[20]={0};
DWORD dwRead;
if(!ReadFile(hFile,(LPVOID)buf,20,&dwRead,NULL)){
printf("ReadFile failed:%d\n",GetLastError());
return 1;
}
printf("content read:%s\n",buf);
CloseHandle(hFile);
return 0;
}
|