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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM