共享方式打开文件

 _fsopen参数说明
 #include<share.h>
 _fsopen 共享模式访问文件

//安全性比fopen高
_fsopen
  以共享的方式打开文件或者流
  FILE *_fsopen(   const char *filename,   const char *mode,   int shflag   );  
filename  Name of the file to open.  //需要打开的文件名
mode   Type of access permitted.  //可以访问的类型
shflag   Type of sharing allowed.  //共享访问类型
 _SH_COMPAT   Sets Compatibility mode for 16-bit applications. //以兼容模式打开16位程序
 _SH_DENYNO   Permits read and write access.  //充许读和写  以此模式打开类似fopen
 _SH_DENYRD   Denies read access to the file.  //拒绝读
 _SH_DENYRW   Denies read and write access to the file.   //拒绝读和写
 _SH_DENYWR   Denies write access to the file   //拒绝写


#include <share.h>
 
int main(void)
{   
     FILE *f1,*f2;
     char s1[256],s2[256];
     //同时打开文件读取
     /*f1=fopen("share.txt","r");
     f2=fopen("share.txt","r");*/
     f1=f2=NULL;
     f1=_fsopen("share.txt","r",_SH_DENYRW);//独占文件访问wb
    
    // f2=_fsopen("share.txt","r",_SH_DENYRW);
     

     if (!f1)
     {
         perror("打开出错");
     }else
     {
         fgets(s1,256,f1);
         printf("%s \n",s1);
     }
      fclose(f1);
     f2=fopen("share.txt","r");
     if (!f2)
     {
         perror("打开出错");
     }else
     {
         fgets(s2,256,f2);
         printf("%s \n",s2);
     }
     //关掉指针
    
     fclose(f2);
    getchar();
    getchar();
    return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值