我要对一个文件实现:一个程序(进程)读,一个程序(进程)写。请教怎么实现

解决方案 »

  1.   

    用共享方式打开即可,例如用CreateFile打开文件,指定dwShareMode为FILE_SHARE_READ|FILE_SHARE_WRITE。
      

  2.   

    不同进程操作同一个文件最好做同步等,Mutex等互斥,这样可以保证数据访问正确等
      

  3.   

    CreateFile与 open有什么区别。有人说用open不好,但是CreateFile应该是用open和sopen实现的啊。请大家帮分析一下。
      

  4.   

    open、sopen都是调用CreateFile实现的。CreateFile是操作系统的API,C/C++库函数最终都是要调用系统API来实现输入/输出的。
      

  5.   

    open、sopen都是调用CreateFile实现的?????
    open sopen应该是C的把.应该比CreateFile底层哦?
      

  6.   

    你自己写段程序,调用open函数,在CreateFile函数上设置一个断点,看会不会运行到。
      

  7.   

    多线程读,使用临界区同步
    #include <afxmt.h>
    void WriteString(CString strLog)
    {
    CString strLogPath = "";
    char *pszPath;
    DWORD nlen = 256;
    pszPath = new char[nlen];
    memset(pszPath,0,sizeof(char)*nlen);
    GetModuleFileName(NULL,pszPath,nlen);
    strLogPath = pszPath;
    strLogPath = strLogPath.Left(strLogPath.ReverseFind('\\')+1);
    strLogPath = strLogPath + "ErrorInfo.log";
    if(pszPath)
    {
    delete []pszPath;
    pszPath = NULL;
    }
    CFile cf;
    OFSTRUCT of;
    HFILE hf = OpenFile(strLogPath,&of,OF_EXIST);
    if(hf==-1)
    {
    cf.Open(strLogPath,CFile::modeCreate|CFile::modeWrite);
    }
    else
    {
    cf.Open(strLogPath,CFile::modeWrite);
    cf.SeekToEnd();
    }
    int iTempValue = GetLastError();
    CString strWrite = "";
    strWrite = strLog + "\r\n";
    CCriticalSection sl;
    sl.Lock();
    cf.Write(strWrite.GetBuffer(strWrite.GetLength()),strWrite.GetLength());
    sl.Unlock();
    cf.Close();
    }