fopen_s遇到的一个问题
今天使用公司代码的日志模块记录程序运行的相关信息,发现日志总是只有两条记录,即程序启动和结束,别的都没有。跟踪了很久,终于发现是日志输出模块被我修改了一个地方:把fopen改成了fopen_s,毕竟报了warning。但是这也是问题的根源!
下面的说明来自于msdn:
Files opened by fopen_s and _wfopen_s are not sharable. If you require that a file be sharable, use _fsopen, _wfsopen with the appropriate sharing mode constant (for example, _SH_DENYNO for read/write sharing).
fopen_s打开的文件不是共享读写的!但是日志模块需要反复在同一个文件中读写,而且每次都调用了fopen_s,第二次调用的时候当然会出错了,错误代码是13,也就是EACCES (Permission denied)
这里应该使用_fsopen:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#include <stdio.h>#include <stdlib.h>#include <share.h>int main( void ){ FILE *stream; // Open output file for writing. Using _fsopen allows us to // ensure that no one else writes to the file while we are // writing to it. // if( (stream = _fsopen( "outfile", "wt", _SH_DENYWR )) != NULL ) { fprintf( stream, "No one else in the network can write " "to this file until we are done.\n" ); fclose( stream ); } // Now others can write to the file while we read it. system( "type outfile" );} |
(以上代码来自于msdn,版权归原作者所有)
fopen_s遇到的一个问题的更多相关文章
- C语言向txt文件写入当前系统时间(Log)
程序实现很简单,涉及到的只有两个知识点. 1.文件最最基本的打开关闭: 2.系统时间的获取. 代码是在VS2019环境下编写的,部分函数在其他编译器中会无法识别.这个就只能需要自己去查找对应的函数简单 ...
- 为什么很多人坚信“富贵险中求”?
之家哥 2017-11-15 09:12:31 微信QQ微博 下载APP 摘要 网贷之家小编根据舆情频道的相关数据,精心整理的关于<为什么很多人坚信"富贵险中求"?>的 ...
- python基础全部知识点整理,超级全(20万字+)
目录 Python编程语言简介 https://www.cnblogs.com/hany-postq473111315/p/12256134.html Python环境搭建及中文编码 https:// ...
- 一个简单的C语言语法检查器的实现
我自己的实现方法的核心过程:首先用一个非终结符代表所有要检查的程序代码,然后根据文法将这个整体的符号不断展开,以拼凑成按检查的程序的顺序排列的终结符序列,能成功说明语法正确,否则有错误. 关键词:分词 ...
- fopen和fopen_s用法的比较 【zz】
在定义FILE * fp 之后,fopen的用法是: fp = fopen(filename,"w").而对于fopen_s来说,还得定义另外一个变量errno_t err,然后e ...
- unix fopen和fopen_s用法的比较
在定义FILE * fp 之后,fopen的用法是: fp = fopen(filename,"w").而对于fopen_s来说,还得定义另外一个变量errno_t err,然后e ...
- 写一个程序,统计自己C语言共写了多少行代码,Github基本操作
前言 在上一篇博客中,本人提到了自己的文件操作可以说是几乎没用过.现在想想,这也算是只在OJ上做题的一个弊端吧.虽然通过OJ做题是一个学习代码好手段,但其他方面也要多多涉猎才好,而不是说OJ用不到文件 ...
- fopen和fopen_s用法的比较
open和fopen_s用法的比较 fopen 和 fopen_s fopen用法: fp = fopen(filename,"w"). fop ...
- 如何判断一个文本文件内容的编码格式 UTF-8 ? ANSI(GBK)
转自:http://blog.csdn.net/jiangqin115/article/details/42684017 UTF-8编码的文本文档,有的带有BOM (Byte Order Mark, ...
随机推荐
- CSS改变插入光标颜色caret-color
CSS代码: input { color: #333; caret-color: red; } @supports (-webkit-mask: none) and (not (caret-color ...
- php图文合成文字居中(png图片合成)
header('Content-type:text/html;charset=utf-8'); /** * png图文合成 by wangzhaobo * @param string $pic_pat ...
- TopCoder SRM500 Div1 500 分治
原文链接https://www.cnblogs.com/zhouzhendong/p/SRM500-500.html SRM500 Div1 500 没想到 double 的精度居然没有爆-- 考虑以 ...
- 删除ELK中的数据。。
直接使用这个命令就行了: curl -XDELETE "http://127.0.0.1:9200/logstash-2017.08.19" 2017.08.19可以动态生成, ...
- Zipkin Server Configuration Using Docker and MySQL[转]
Zipkin is a used for capturing timing data, it also has a centralized repository, and a microweb ser ...
- Eclipse 无输出,但不报错
解决方法: 若界面中都没有console选项,则 工具栏 Window - Show View - Console Window - Preferences - Run/Debug - Console ...
- day41 mycql 函数
一些经典的练习题,以及函数的简单用法,內建函数 -- 函数 python函数 def fun1(a1,a2,a3): sum = a1+a2+a3 return sum fun1(1,2,3) jav ...
- KMP算法2
给定一个主串s,一个子串sub,将主串中的所有子串替换成replaceStr,并将最终结果输出来. #include<stdio.h> #include<string.h> # ...
- M × N Puzzle POJ - 2893(奇数码)
The Eight Puzzle, among other sliding-tile puzzles, is one of the famous problems in artificial inte ...
- 阿里 EasyExcel 使用及避坑
github地址:https://github.com/alibaba/easyexcel 原本在项目中使用EasyPoi读取excel,后来为了统一技术方案,改用阿里的EasyExcel.EasyE ...