访问文件时遇到共享冲突的问题

phisherr 2010-02-27 02:22:30
各位好:
我来问个问题:我有一个程序,有多个实例进程同时运行,在运行中会访问一个文件“xxx.xxx”,这个文件很小,只有几k。在运行过程会弹出一个消息框,内容为“访问xxx.xxx时遇到共享冲突”,然后进程就在这里卡住了,点确定后还能继续运行。

读文件是用CFile读取的,属性用了CFile::modeRead | CFile::shareDenyNone

这个问题怎么解决呢?
...全文
13237 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lmxmx 2010-02-28
  • 打赏
  • 举报
回复
引用 7 楼 hzy694358 的回复:
打开后,要关闭,否则再打开就会出错


yhp1888 2010-02-28
  • 打赏
  • 举报
回复
第一,你设置属性时疏忽了shareDenyNone和shareDenyRead的关系,不同的实例进程使用了不同的打开属性。

第二,如果疏忽了打开属性,每个实例进程打开文件“xxx.xxx”后,如果都及时关闭了,其它实例进程再打开时也不会出问题。
phisherr 2010-02-28
  • 打赏
  • 举报
回复
谢谢大家,其实是个误会。属性用了CFile::modeRead | CFile::shareDenyNone 就没有问题
hzy694358 2010-02-27
  • 打赏
  • 举报
回复
打开后,要关闭,否则再打开就会出错
chenzhuo8804 2010-02-27
  • 打赏
  • 举报
回复
我觉得楼上应该是正解, 应该是多次打开的问题, 推荐编写一个文件的shell , 每次打开的时候都会顺带关闭
WizardK 2010-02-27
  • 打赏
  • 举报
回复
引用楼主 phisherr 的回复:
各位好:
我来问个问题:我有一个程序,有多个实例进程同时运行,在运行中会访问一个文件“xxx.xxx”,这个文件很小,只有几k。在运行过程会弹出一个消息框,内容为“访问xxx.xxx时遇到共享冲突”,然后进程就在这里卡住了,点确定后还能继续运行。

读文件是用CFile读取的,属性用了CFile::modeRead | CFile::shareDenyNone

这个问题怎么解决呢?


你是不是这个程序执行过多次?每次是不是没有Close?
PointertoPointer 2010-02-27
  • 打赏
  • 举报
回复
应该再查一查MSDN

CFile::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.

cpp2017 2010-02-27
  • 打赏
  • 举报
回复
确定所有的进程都是用CFile::modeRead | CFile::shareDenyNone 打开这个文件的?
dukelee 2010-02-27
  • 打赏
  • 举报
回复
如果别的程序用专有方式打开这个文件,就会冲突,文件读取失败
BombZhang 2010-02-27
  • 打赏
  • 举报
回复
是不是有其他地方用shareDenyRead方式打开这个文件了?
Re:  FastDFS 分布式文件系统部署 ============================== FastDFS 分布式存储主要功能有:文件存储,文件同步,文件访问文件上传/下载),特别适合以文件为载体的在线服务,如图片网站,视频网站等 它的存储特点是对文件体积小或超小,同文件的数量特别多的存储情况支持较好,(如果是大文件的存储就推荐使用 Glusterfs 分布式网络文件存储),所谓的小文件就是指网站的图片,文档或者小视频等等,体积范围在4K~500MB之间。所谓的大文件指软件的镜像包、电影等等。 除了FastDFS存储文件特点之外,它同具备自我负载均衡的能力,可以解决了运维自动化的关键问题。 # 块存储、文件存储、对象存储这三者的本质差别是什么? 1)块存储:磁盘阵列,硬盘(块存储主要是将裸磁盘空间整个映射给主机使用的) 2)文件存储:FTP、NFS服务器(解决文件无法共享问题) 3)对象存储:内置大容量硬盘的分布式服务器(解决容量、性能、可靠性等问题) 多台服务器内置大容量硬盘,再装上对象存储软件, 然后再额外搞几台服务作为管理节点,安装上对象存储管理软件。 # gluterfs主要运行场景: 存储大文件,针对小文件或超小文件的支持比较差 KVM镜像 视频文件 # FastDFS主要运行场景: 存放文件size范围:4K~500MB # 对象存储 - GlusterFS、Ceph、FastDFS(非对象存储) 必备技能:DRBD/NFS/MooseFS/ GlusterFS/ FastDFS/TFS(http://tfs.taobao.org/)/ # 访问客户端:只能专用API访问实现 存放单元文件size范围:4K~500MB(阅读、声音、视频网站) # 典型用户:      ... ... 

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧