免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1531 | 回复: 5
打印 上一主题 下一主题

请高手帮忙看段简单代码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-17 10:09 |只看该作者 |倒序浏览
#include <stdio.h>
int main()
{
char *fname="cv.txt";
char *cv_content;

FILE *fp;

if((fp=fopen(fname,"r"))==NULL)
{
printf("Error!--Can't open file\n");
return 0;
}
fscanf(fp,"%s",cv_content);
fclose(fp);
printf("The cv_content is :\n%s",cv_content);
return 0;
}

用gcc -o 编译
运行以后报错
Segmentation fault
想不明白。

论坛徽章:
0
2 [报告]
发表于 2009-08-17 10:17 |只看该作者
char *cv_content = malloc(1234);

论坛徽章:
0
3 [报告]
发表于 2009-08-17 10:22 |只看该作者

回复 #2 prc 的帖子

warning: initialization makes pointer from integer without a cast

我试了你给出的语句,出现了警告,但是程序可以正常运行。

论坛徽章:
0
4 [报告]
发表于 2009-08-17 10:29 |只看该作者
没分配空间当然会core

论坛徽章:
0
5 [报告]
发表于 2009-08-17 11:45 |只看该作者
原帖由 Linux的追求者 于 2009-8-17 10:09 发表
#include
int main()
{
char *fname="cv.txt";
char *cv_content;

FILE *fp;

if((fp=fopen(fname,"r"))==NULL)
{
printf("Error!--Can't open file\n");
return 0;
}
fscanf(fp,"%s",cv_conten ...

cv_content的空间都没分配。。。

论坛徽章:
0
6 [报告]
发表于 2009-08-17 13:52 |只看该作者
Programmers new to C usally neglect the fact that they must handle memory allocation manually, via malloc(), calloc(), realloc() and free().
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP