[问题] 简单的fgetc fputc为什么运行时报SIGSEGV?

C、C++和Java语言
回复
rqliang
帖子: 16
注册时间: 2007-02-16 15:39
送出感谢: 0
接收感谢: 0

[问题] 简单的fgetc fputc为什么运行时报SIGSEGV?

#1

帖子 rqliang » 2007-07-30 17:40

很简单的
while((c=fgetc(fp))!=EOF){
if (c=='\t') c=',';
fputc(c, fp2);
}
gcc 编译没报错,为什么执行的时候报段错误,gdb调试报
Program received signal SIGSEGV, Segmentation fault.
0xb7ea521b in getc () from /lib/tls/i686/cmov/libc.so.6
求助!

uname -r
2.6.20-16-generic
头像
nobrain
帖子: 808
注册时间: 2005-08-25 13:58
来自: ustc
送出感谢: 0
接收感谢: 0
联系:

#2

帖子 nobrain » 2007-07-30 18:00

应该是访问了不该访问的内存,不知你的fp,fp2都指向哪里?
爱喝真猪奶茶的夜鸣猪
rqliang
帖子: 16
注册时间: 2007-02-16 15:39
送出感谢: 0
接收感谢: 0

#3

帖子 rqliang » 2007-07-30 18:23

指向我自己的目录里的普通文件啊。
头像
aitilang
帖子: 1026
注册时间: 2007-04-28 21:38
送出感谢: 0
接收感谢: 0

#4

帖子 aitilang » 2007-07-30 18:40

把fp的定义贴上来。。
rqliang
帖子: 16
注册时间: 2007-02-16 15:39
送出感谢: 0
接收感谢: 0

#5

帖子 rqliang » 2007-07-30 18:50

#include <stdio.h>

FILE *input, *output;
int c;

int main(int argc, char ** argv){
if(argc!=3){
fprintf(stderr,"Usage: %s filename.gpr filename.csv\n", argv[0]);
exit(1);
}
if((input=fopen(argv[1],"r")==NULL)){
fprintf(stderr,"Cannot open %s.", argv[1]);
exit(1);
}
if((output=fopen(argv[2],"w")==NULL)){
fprintf(stderr,"Cannot write to %s\n", argv[2]);
exit(1);
}
while((c=fgetc(input))!=EOF){
if (c == '\t') c=',';
fputc(c,output);
}
fclose(input);
fclose(output);
return 0;
}
rqliang
帖子: 16
注册时间: 2007-02-16 15:39
送出感谢: 0
接收感谢: 0

#6

帖子 rqliang » 2007-07-30 18:52

t$ ls
2007-07-06_liang-heart-s5-800_0635_R1.jpg gpr2csv.py
2007-07-06_liang-heart-s5-800_0635_W1.jpg gpr2cvs.c
2007-07-06_liang-heart-s5-800_labeled.jpg gpr2cvs.c~
data.gpr liang-heart-s5-800_635.tif
g2c
liang@liang-dv1381tu:~/Documents/Experiments/Ana-with-GenePix/liang20070706/heart$ gdb g2c
GNU gdb 6.6-debian
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1".
(gdb) set args data.gpr data.csv
(gdb) r
Starting program: /home/liang/Documents/Experiments/Ana-with-GenePix/liang20070706/heart/g2c data.gpr data.csv

Program received signal SIGSEGV, Segmentation fault.
0xb7e1021b in getc () from /lib/tls/i686/cmov/libc.so.6
rqliang
帖子: 16
注册时间: 2007-02-16 15:39
送出感谢: 0
接收感谢: 0

#7

帖子 rqliang » 2007-07-30 18:54

先多谢您的耐心帮助1 :em51
回复

回到 “C/C++/Java”