这个程序哪里有问题

wohu007 2011-03-30 10:47:28
实现字符的转化,即把字符转化为他后面的第五个字符
#include "stdio.h"
#define N 10
main()
{ FILE *fp;
char str[N];
int i;
if(fp=fopen("text.txt","w")==NULL)
{ printf("cannot open this file\n");
exit(0);
}
printf("请输入字符串\n");
gets(str);
for(i=0;i<N;i++)
{ if(str[i]!='\0')
str[i]+=5;
}
fwrite(str,sizeof(char),N,fp);
rewind(fp);
fread(str,sizeof(char),N,fp);
puts(str);
fclose(fp);
}
...全文
97 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
DennisCPP 2011-03-30
  • 打赏
  • 举报
回复
if(fp=fopen("text.txt","w")==NULL)
lz看这句,==的优先级是要比=的优先级高的,所以先计算fopen("text.txt","w")==NULL,这里返回一个布尔值,然后把这个布尔值赋值给fp,fp是一个指针类型,即FILE *,所以这里出现了错误,可以这么写
if((fp=open("text.txt","w"))==NULL)
無_1024 2011-03-30
  • 打赏
  • 举报
回复
注意优先级啊 加一个括号和一个头文件就可以了
無_1024 2011-03-30
  • 打赏
  • 举报
回复

#include "stdio.h"
#include <stdlib.h>
#define N 10
int main()
{ FILE *fp;
char str[N];
int i;
if((fp=fopen("text.txt","w"))==NULL)
{ printf("cannot open this file\n");
exit(0);
}
printf("请输入字符串\n");
gets(str);
for(i=0;i<N;i++)
{ if(str[i]!='\0')
str[i]+=5;
}
fwrite(str,sizeof(char),N,fp);
rewind(fp);
fread(str,sizeof(char),N,fp);
puts(str);
fclose(fp);
return 0;
}

wariice 2011-03-30
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>
这样呢?
c_losed 2011-03-30
  • 打赏
  • 举报
回复

#include "stdio.h"
#include <Windows.h>
#define N 10
void main()
{
FILE *fp;
char str[N];
int i;
fp=fopen("text.txt","w");
if(!fp)
{
printf("cannot open this file\n");
exit(0);
}
printf("请输入字符串\n");
gets(str);
for(i=0;i<N;i++)
{ if(str[i]!='\0')
str[i]+=5;
}
fwrite(str,sizeof(char),N,fp);
rewind(fp);
fread(str,sizeof(char),N,fp);
puts(str);
fclose(fp);
}

上面倒可以编译通过 其他问题未仔细看
c_losed 2011-03-30
  • 打赏
  • 举报
回复
1>------ Build started: Project: US, Configuration: Debug Win32 ------
1>Compiling...
1>as.cpp
1>f:\code\us\us\as.cpp(4) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>f:\code\us\us\as.cpp(7) : error C2440: '=' : cannot convert from 'bool' to 'FILE *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>f:\code\us\us\as.cpp(9) : error C3861: 'exit': identifier not found
  • 打赏
  • 举报
回复
莫非是头文件问题!

69,373

社区成员

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

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