能力值:
( LV4,RANK:50 )
|
-
-
2 楼
个人猜测 一个句柄应该只有1个文件位置指针 线程另外CreateFile一个试试
使用映射的话直接操作内存就可以了 完了UnmapViewOfFile 系统会更新文件的
|
能力值:
( LV6,RANK:80 )
|
-
-
3 楼
#include<windows.h>
#include<process.h>
#include<iostream>
using namespace std;
#define BUFSIZE 5
DWORD __stdcall CreateThreadFunc(LPVOID)
{
cout<<"进入子线程"<<endl;
OFSTRUCT os={sizeof(OFSTRUCT)};
HANDLE hFile=(HANDLE)OpenFile("test.txt",&os,OF_READWRITE);
cout<<GetLastError()<<endl;
SetFilePointer(hFile,5,0,FILE_BEGIN);
char chBuffer[BUFSIZE];
memcpy(chBuffer,"a boy",5);
DWORD dwWriteSize=0;
WriteFile(hFile,chBuffer,5,&dwWriteSize,NULL);
CloseHandle(hFile);
return 0;
}
int main()
{
HANDLE hFile=CreateFile("test.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
cout<<GetLastError()<<endl;
//SetFileValidData(hFile,4);
HANDLE hFileMap=CreateFileMapping(hFile,NULL,PAGE_READWRITE,0,10,NULL);
cout<<GetLastError()<<endl;
char chBuffer[BUFSIZE];
memcpy(chBuffer,"yirui",5);
DWORD dwWriteSize=0;
SetFilePointer(hFile,0,0,FILE_BEGIN);
WaitForSingleObject(CreateThread(NULL,0,CreateThreadFunc,0,0,0),INFINITE);
WriteFile(hFile,chBuffer,5,&dwWriteSize,NULL);
CloseHandle(hFileMap);
CloseHandle(hFile);
return 0;
}
一个文件可以定义多个文件指针而互不干扰彼此,但是一个文件句柄就只能对应一个文件指针。
|