zoukankan      html  css  js  c++  java
  • 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 
    ",s1);
         }
          fclose(f1);
         f2=fopen("share.txt","r");
         if (!f2)
         {
             perror("打开出错");
         }else
         {
             fgets(s2,256,f2);
             printf("%s 
    ",s2);
         }
         //关掉指针
        
         fclose(f2);
        getchar();
        getchar();
        return 0;
    }
  • 相关阅读:
    ※剑指offer系列51:二叉搜索树的第k个结点
    ※剑指offer系列50:序列化二叉树
    sqlserver添加表注释、字段注释
    3-实体数据模型与LINQ-where&OfType
    3-实体数据模型与LINQ-Select
    Jquery 在子页面上设置父页面元素的值
    开发注意事项
    函数的进阶
    文件操作的相关内容
    基本数据类型----dict
  • 原文地址:https://www.cnblogs.com/whzym111/p/6163594.html
Copyright © 2011-2022 走看看