看看这个程序怎么调试不了

ruchong001 2008-01-27 10:16:26
#include <stdio.h>
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);


}
C:\Documents and Settings\zhuandchang\11.cpp(6) : 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
C:\Documents and Settings\zhuandchang\11.cpp(9) : error C2065: 'exit' : undeclared identifier
Error executing cl.exe.

11.exe - 2 error(s), 0 warning(s)
来帮我看看呀?为什么错误




...全文
152 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ruchong001 2008-01-29
  • 打赏
  • 举报
回复
解释的很精辟啊,我懂了,多谢啊
yang511yang 2008-01-28
  • 打赏
  • 举报
回复
#include <stdio.h>
void main()
{
FILE *fp;
char ch='a';
if ((fp=fopen("text1","w"))==NULL)//赋值运算符=的优先级那可是相当的低,是倒数几位,所以要加括号
{
printf("can not open file\n");
return;
}
fputc(ch,fp);
fclose(fp);
visame 2008-01-27
  • 打赏
  • 举报
回复
#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 *的真正结构吧!
*/

wuyu637 2008-01-27
  • 打赏
  • 举报
回复
不加括号的话,运算符的优先级不够,不能按照你想的顺序计算。
wuyu637 2008-01-27
  • 打赏
  • 举报
回复
#include <stdlib.h>
ruchong001 2008-01-27
  • 打赏
  • 举报
回复
还有啊,为什么要加括号?不加不行么?
ruchong001 2008-01-27
  • 打赏
  • 举报
回复
找到一个错误.还有一个
C:\Documents and Settings\zhuandchang\1.cpp(9) : error C2065: 'exit' : undeclared identifier
NKLoveRene 2008-01-27
  • 打赏
  • 举报
回复
加个括号
if   ( (fp=fopen("text1","w")) ==NULL)

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧