各位好:
我来问个问题:我有一个程序,有多个实例进程同时运行,在运行中会访问一个文件“xxx.xxx”,这个文件很小,只有几k。在运行过程会弹出一个消息框,内容为“访问xxx.xxx时遇到共享冲突”,然后进程就在这里卡住了,点确定后还能继续运行。读文件是用CFile读取的,属性用了CFile::modeRead | CFile::shareDenyNone这个问题怎么解决呢?

解决方案 »

  1.   

    是不是有其他地方用shareDenyRead方式打开这个文件了?
      

  2.   

    确定所有的进程都是用CFile::modeRead | CFile::shareDenyNone 打开这个文件的?
      

  3.   

    应该再查一查MSDNCFile::modeCreate   Directs the constructor to create a new file. If the file exists already, it is truncated to 0 length.CFile::modeNoTruncate   Combine this value with modeCreate. If the file being created already exists, it is not truncated to 0 length. Thus the file is guaranteed to open, either as a newly created file or as an existing file. This might be useful, for example, when opening a settings file that may or may not exist already. This option applies to CStdioFile as well.CFile::modeRead   Opens the file for reading only.CFile::modeReadWrite   Opens the file for reading and writing.CFile::modeWrite   Opens the file for writing only.CFile::modeNoInherit   Prevents the file from being inherited by child processes.CFile::shareDenyNone   Opens the file without denying other processes read or write access to the file. Create fails if the file has been opened in compatibility mode by any other process.CFile::shareDenyRead   Opens the file and denies other processes read access to the file. Create fails if the file has been opened in compatibility mode or for read access by any other process.CFile::shareDenyWrite   Opens the file and denies other processes write access to the file. Create fails if the file has been opened in compatibility mode or for write access by any other process.CFile::shareExclusive   Opens the file with exclusive mode, denying other processes both read and write access to the file. Construction fails if the file has been opened in any other mode for read or write access, even by the current process.CFile::shareCompat   This flag is not available in 32 bit MFC. This flag maps to CFile::shareExclusive when used in CFile::Open.CFile::typeText   Sets text mode with special processing for carriage return–linefeed pairs (used in derived classes only).CFile::typeBinary   Sets binary mode (used in derived classes only).CFile::osNoBuffer   The system opens a file with no system caching. For more information see FILE_FLAG_NO_BUFFERING in CreateFile in the Platform SDK.CFile::osWriteThrough   The system writes through any intermediate cache and goes directly to disk. For more information see FILE_FLAG_WRITE_THROUGH in CreateFile in the Platform SDK.CFile::osRandomAccess   A file is accessed randomly. The system can use this as a hint to optimize file caching.CFile::osSequentialScan   A file is accessed sequentially from beginning to end. The system can use this as a hint to optimize file caching. For more information see FILE_FLAG_SEQUENTIAL_SCAN in CreateFile in the Platform SDK.
      

  4.   


    你是不是这个程序执行过多次?每次是不是没有Close?
      

  5.   

    我觉得楼上应该是正解, 应该是多次打开的问题, 推荐编写一个文件的shell , 每次打开的时候都会顺带关闭
      

  6.   

    第一,你设置属性时疏忽了shareDenyNone和shareDenyRead的关系,不同的实例进程使用了不同的打开属性。第二,如果疏忽了打开属性,每个实例进程打开文件“xxx.xxx”后,如果都及时关闭了,其它实例进程再打开时也不会出问题。
      

  7.   

    谢谢大家,其实是个误会。属性用了CFile::modeRead | CFile::shareDenyNone 就没有问题