求大神解答:
给一个csv文件建立内存映射文件,映射后如果不用excel打开该文件,可以正常读取,如果用excel打开,读取到的内容全是0,
但是运行过程中没发生任何win32调用错误,也没有返回NULL,我该如何
检测该文件已被excel打开,然后提示先将其关闭?
[C] 纯文本查看 复制代码 LPVOID LoadFileR(LPTSTR lpFilename, DWORD dwSize)
{
HANDLE hFile;
HANDLE hMapping;
LPVOID ImageBase;
hFile= CreateFile(lpFilename,GENERIC_WRITE|GENERIC_READ ,0,NULL,OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,0);
if (NULL == hFile){
return NULL;
}
hMapping= CreateFileMapping(hFile,NULL,PAGE_READWRITE ,0,dwSize,NULL);
if (NULL == hMapping)
{
int i = GetLastError();
CloseHandle(hFile);
return NULL;
}
ImageBase= MapViewOfFile(hMapping,FILE_MAP_WRITE,0,0,0);
CloseHandle(hFile);
CloseHandle(hMapping);
return ImageBase;
}
char* easycsv_csv_loadfile(char* path,unsigned long* psize){
FILE *fp;
unsigned long flen;
if((fp = fopen (path,"r"))==NULL){
return NULL;
}
fseek(fp,0L,SEEK_END);
flen=ftell(fp);
fclose(fp);
*psize = flen;
return (char*)LoadFileR(path,flen);
}
int main(){
int i;
unsigned long flen = 0;
char* csv = easycsv_csv_loadfile("tst.csv",&flen);
printf("csv=%u\n",(unsigned long)csv);
char* p = csv;
for(i=0;i<flen;++i){
printf("p=%c\n",*p++);
}
return 1;
} |