分页: 1 / 1

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

发表于 : 2007-07-30 17:40
rqliang
很简单的
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

发表于 : 2007-07-30 18:00
nobrain
应该是访问了不该访问的内存,不知你的fp,fp2都指向哪里?

发表于 : 2007-07-30 18:23
rqliang
指向我自己的目录里的普通文件啊。

发表于 : 2007-07-30 18:40
aitilang
把fp的定义贴上来。。

发表于 : 2007-07-30 18:50
rqliang
#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;
}

发表于 : 2007-07-30 18:52
rqliang
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

发表于 : 2007-07-30 18:54
rqliang
先多谢您的耐心帮助1 :em51