我的小程序是这样的:1.在main里面CreateFile打开一个文件
2.启动一个新的线程来Write一个字符串helloworld
3.在main里面等待线程完成,把内容读出来。但是Write的内容好像没有被写入硬盘,程序打印Read的内容为空。程序结束以后也没有磁盘文件生成。这到底是为什么呢? 我要求程序不要多次CreateFile/OpenFile,只打开一次,不同的线程执行读/写操作。
我应该如何修改? 请指点。程序代码如下:#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());
}
return NULL;
}
int _tmain(int argc, _TCHAR* argv[])
{
hFile=CreateFile("d:\\my.txt",
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_WRITE|FILE_SHARE_READ,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_HIDDEN,
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);
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;
}

解决方案 »

  1.   

    每次写完close文件 然后读
      

  2.   


    #include "stdafx.h"
    #include<windows.h>
    #include<stdio.h>
    HANDLE hFile;
    void* fun(void*){
    TCHAR hw[]= _T("helloworld");
    DWORD dwWriten;
    if(!WriteFile(hFile,(LPVOID)hw,sizeof(hw),&dwWriten,NULL)){//ERROR_ACCESS_DENIED(5)
    printf("WriteFile failed:%d\n",GetLastError());
    }
    return NULL;
    }
    int _tmain(int argc, _TCHAR* argv[])
    {
    hFile=CreateFile(_T("F:\\my.txt"),
    GENERIC_READ|GENERIC_WRITE,
    FILE_SHARE_WRITE|FILE_SHARE_READ,
    NULL,
    CREATE_ALWAYS,
    FILE_ATTRIBUTE_HIDDEN,
    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);
    CloseHandle(hFile); TCHAR buf[20]={0};
    DWORD dwRead;
    hFile=CreateFile(_T("F:\\my.txt"),
    GENERIC_READ,
    FILE_SHARE_READ,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_HIDDEN,
    NULL);
    if(!ReadFile(hFile,(LPVOID)buf,sizeof(buf),&dwRead,NULL)){
    printf("ReadFile failed:%d\n",GetLastError());
    return 1;
    }
    _tprintf(_T("content read:%s\n"),buf);
    CloseHandle(hFile);
    return 0;
    }
      

  3.   

    FlushFileBuffers(HANDLE)HANDLE CReateFile();有这么个函数,没用过~Cfile的话就直接用CFile::Flush();了
      

  4.   


    大虾,可不可以,不需要两次CreateFile,CloseHandle,就完成我的需求呢?
      

  5.   

    我的需求说白了就是,同一个文件句柄,被不同线程来执行读/写。不要多次CreateFile来得到不同的Handle.我修改了一下代码,加入了FlushFileBuffers,但是还是一样的运行结果。现在的代码是:#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,
    CREATE_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);
    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;
    }
      

  6.   


    #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(_T("F:\\my.txt"),
    GENERIC_READ|GENERIC_WRITE,
    FILE_SHARE_WRITE|FILE_SHARE_READ,
    NULL,
    CREATE_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;
    }
      

  7.   

    WaitForSingleObject(hThread,INFINITE);
    后面加上这句
    SetFilePointer(hFile, 0, 0, FILE_BEGIN);
      

  8.   


    Bingo,这个就能搞定!谢谢!