c - C 中 if ....==NULL 的问题
问题描述
我有这个代码,注意它被缩短了。问题是如果文件存在,它仍然会覆盖它。自从我做任何编程以来已经 30 年了,所以请耐心等待。谢谢!
FILE *openFil(FILE *open, char namn[]);
int main(int argc, const char * argv[])
{
FILE *openFil(FILE *open, char namn[]);
FILE *anmal =NULL;
char filNamn[] = "ANMAL.DAT";
postTypAnm pAnm;
anmal = openFil(anmal, filNamn);
}
FILE *openFil(FILE *pointer, char name[])
{
if ((pointer =fopen(name, "r+b"))== NULL)
if ((pointer =fopen(name, "w+b"))== NULL)
{
/* It Enters here as well, but it should not do that or????? */
printf("error\n");
exit(0);
}
return pointer;
}
解决方案
如果您使用的是 C11 标准,则可以使用“x”参数来指定如果文件存在,则fopen()
函数将失败。
推荐阅读
- ios - 为什么只显示集合视图的一半?
- python - 无法从 anaconda 启动 Jupyter
- android - How can a Delagetes.observable in a BroadcastReceiver be unit tested?
- amazon-s3 - 使用 boto3 键错误从 S3 下载文件
- asp.net-core - .netcore web API Post 在没有 SSL 的情况下无法工作
- c# - RestSharp 请求中的错误?, XML , ':' 字符,十六进制值 0x3A,不能包含在名称中
- java - Spring @ConfigurationProperties 解析器忽略外部化属性
- arrays - 在一行中使用 /bin/sh 声明一个数组
- java - 将 JBOSS EAP 7.1.5 迁移到 JBOSS EAP 7.2 时出现休眠问题
- node.js - 将下一个 js 更新到 v9 后链接错误 - 参数 'url' 必须是字符串,而不是未定义