69,373
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>//exit()函数的需要
void main()
{
FILE *fp;
char ch='a';
if ((fp=fopen("text1","w"))==NULL)//赋值运算符=的优先级那可是相当的低,是倒数几位,所以要加括号
{
printf("can not open file\n");
exit(0);
}
fputc(ch,fp);
fclose(fp);
/*
(fp=fopen("text1","w"))==NULL如果不加括号,就是fp=fopen("text1","w")==NULL
根据运算符优先级:fp=(fopen("text1","w")==NULL)。==先算,运算的结果是bool型常量true/false。
于是变成fp=true;或者fp=false;
于是提示错误,
error C2440: '=' : cannot convert from 'bool' to 'struct _iobuf *'Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
这个错误的意识是说:true/false是bool型,而fp是FILE *类型,不能进行赋值运算阿!
这个struct _iobuf *应该是File *的真正结构吧!
*/
if ( (fp=fopen("text1","w")) ==NULL)