段错误,帮忙找BUG

kestre1 2009-02-12 12:33:57
这个程序要求根据输入文件(in.txt)里的内容,找出其中最长的十个句子。测试的时候发现当输入文件内容比较少的时候没问题,当超过一定长度,运行的时候就是段错误,看了半天也没找出什么原因。
环境:2.6.27-11-generic,gcc-4.3.2

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAXLEN 2048
#define LINENUM 10
struct _Line{
int num; /*序号*/
int len; /*长度*/
char *str; /*字符串*/
};

int main(int argc, char **argv){
char buff[MAXLEN];
struct _Line line[LINENUM];
int num = 0; /*读入行序列号*/
int minlen = 0; /*当前缓冲区最短行长度*/
int minnum; /*当前缓冲区最短行序号*/
int i;
/*读入文件*/
FILE *in;
if ( (in = fopen("in.txt", "r")) == NULL){
perror("fopen");
exit(1);
}
while(fgets(buff, MAXLEN, in) != NULL){
/*当输入行小于10行的情况*/
if(num < LINENUM){
line[num].num = num;
line[num].len = strlen(buff);

line[num].str = (char *)malloc(MAXLEN * sizeof(char));
strncpy(line[num].str, buff, strlen(buff) + 1);
line[num].str[strlen(buff) - 1] = '\0';
}else{
/*找出当前缓冲区最短行*/
minlen = line[0].len;
minnum = 0;
for (i = 0; i < LINENUM; i++){
if(line[i].len < minlen){
minlen = line[i].len;
minnum = line[i].num;
}
}
/*根据最短行的行号替换*/
if (strlen(buff) > minlen ){
strncpy(line[minnum].str, buff, strlen(buff) + 1);
line[minnum].str[strlen(buff) - 1] = '\0';
line[minnum].len = strlen(buff);
line[minnum].num = num;

}

}
num++;
}
/*输出*/
printf("Top 10 lines:\n");
for(i = 0; i < (LINENUM > num ? num: LINENUM); i++){
printf("No.%2d Length:%3d:%s\n", line[i].num, line[i].len, line[i].str);
free(line[i].str);
}

return 0;
}
...全文
275 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
kestre1 2009-02-12
  • 打赏
  • 举报
回复
哪位仁兄帮忙测试一下也行阿~
kestre1 2009-02-12
  • 打赏
  • 举报
回复
太帅了,谢谢各位达人,结贴去了~
baihacker 2009-02-12
  • 打赏
  • 举报
回复
            for (i = 0; i < LINENUM; i++){
if(line[i].len < minlen){
minlen = line[i].len;
minnum = i;//改这里
}
}
pacman2000 2009-02-12
  • 打赏
  • 举报
回复
/*根据最短行的行号替换*/
if (strlen(buff) > minlen ){
strncpy(line[minnum].str, buff, strlen(buff) + 1);
line[minnum].str[strlen(buff) - 1] = '\0';
line[minnum].len = strlen(buff);
line[minnum].num = num;

关键在这里:找到替换的行以后,你设置了line[minnum].num = num; 这个num肯定是大于10的!!!
结果,下次再找最短行时,如果就是这行,
for (i = 0; i < LINENUM; i++){
if(line[i].len < minlen){
minlen = line[i].len;
minnum = line[i].num;
}
}
这时候 minnum 就是一个大于10的数。。。于是,在下面strncpy(line[minnum].str, buff, strlen(buff) + 1); 就段错误了。
chenqiang35 2009-02-12
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 Leejun527 的回复:]
就这一句得问题,minnum = line[i].num;
改成minnum = i;就好了
自己好好想想,你这个num存得是行号而不line数据中得索引号。
[/Quote]

是的
Leejun527 2009-02-12
  • 打赏
  • 举报
回复
就这一句得问题,minnum = line[i].num;
改成minnum = i;就好了
自己好好想想,你这个num存得是行号而不line数据中得索引号。
ltc_mouse 2009-02-12
  • 打赏
  • 举报
回复
/*根据最短行的行号替换*/
if (strlen(buff) > minlen ){
strncpy(line[minnum].str, buff, strlen(buff) + 1);
line[minnum].str[strlen(buff) - 1] = '\0';
line[minnum].len = strlen(buff);
line[minnum].num = num; ///这条语句是否会造成越界访问?个人认为可删去~
}
fibbery 2009-02-12
  • 打赏
  • 举报
回复
gcc -g your.cpp
gdb your
r your_param ...
找到段错误的行,然后分析代码
iambic 2009-02-12
  • 打赏
  • 举报
回复
debug...
kestre1 2009-02-12
  • 打赏
  • 举报
回复
测试文件就是在网上随便找的一段,然后复制了几次

NEW YORK (CNN) -- A pedestrian was struck twice by vehicles in Queens and dragged for 17 miles by the second vehicle before police found him dead in Brooklyn, New York City Police Commissioner Ray Kelly told reporters Wednesday.
The badly mangled body was discovered under a van after several passing motorists motioned the driver to pull over, Kelly said.
Police had not identified the victim Wednesday evening, he said.
The first driver called 911 to report he thought he had struck a pedestrian but did not see anyone.
It turned out the second driver, in a van, had driven over the man, whose body became lodged under its chassis, according to police.

Kelly said that the van driver stopped at one point during the drive on New York City's highways and roads because he noticed the vehicle was not driving properly.
But he failed to find anything unusual, Kelly said.

Police are retracing the route the van drove in attempt to find body parts, he said.

No charges have been filed, Kelly told reporters Wednesday afternoon.
NEW YORK (CNN) -- A pedestrian was struck twice by vehicles in Queens and dragged for 17 miles by the second vehicle before police found him dead in Brooklyn, New York City Police
NEW YORK (CNN) -- A pedestrian was struck twice by vehicles in Queens and dragged for 17 miles by the second vehicle before police found him dead in Brooklyn, New York City Police Commissioner Ray Kelly told reporters Wednesday.
The badly mangled body was discovered under a van after several passing motorists motioned the driver to pull over, Kelly said.
Police had not identified the victim Wednesday evening, he said.
The first driver called 911 to report he thought he had struck a pedestrian but did not see anyone.
It turned out the second driver, in a van, had driven over the man, whose body became lodged under its chassis, according to police.

Kelly said that the van driver stopped at one point during the drive on New York City's highways and roads because he noticed the vehicle was not driving properly.
But he failed to find anything unusual, Kelly said.

Police are retracing the route the van drove in attempt to find body parts, he said.

No charges have been filed, Kelly told reporters Wednesday afternoon.
NEW YORK (CNN) -- A pedestrian was struck twice by vehicles in Queens and dragged for 17 miles by the second vehicle before police found him dead in Brooklyn, New York City Police
NEW YORK (CNN) -- A pedestrian was struck twice by vehicles in Queens and dragged for 17 miles by the second vehicle before police found him dead in Brooklyn, New York City Police Commissioner Ray Kelly told reporters Wednesday.
The badly mangled body was discovered under a van after several passing motorists motioned the driver to pull over, Kelly said.
Police had not identified the victim Wednesday evening, he said.
The first driver called 911 to report he thought he had struck a pedestrian but did not see anyone.
It turned out the second driver, in a van, had driven over the man, whose body became lodged under its chassis, according to police.

Kelly said that the van driver stopped at one point during the drive on New York City's highways and roads because he noticed the vehicle was not driving properly.
But he failed to find anything unusual, Kelly said.

Police are retracing the route the van drove in attempt to find body parts, he said.

No charges have been filed, Kelly told reporters Wednesday afternoon.
NEW YORK (CNN) -- A pedestrian was struck twice by vehicles in Queens and dragged for 17 miles by the second vehicle before police found him dead in Brooklyn, New York City Police
wudeshou82666 2009-02-12
  • 打赏
  • 举报
回复
系统环境不同,不好测试
chenqiang35 2009-02-12
  • 打赏
  • 举报
回复
你的测试文件也发上来下

70,023

社区成员

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

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