c++ 文件共享打开

 _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;
}