求助,段错误怎么办

lybcyd 2012-04-04 11:32:43
#include "std.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int len = 0;

struct node
{
char *word;
int count;
struct node *next;
}*head,*p,*q;

struct node* printf_sort(struct node *head)
{
struct node *p1,*p2,*ptemp,*pfinished = NULL;
for (p1 = head; p1->next != pfinished;)
{
for (p2 = p1; p2->next != pfinished;)
{
if (p2->count < p2->next->count)
{
if (p2 == p1)
{
p1 = p2->next;
p2->next = p1->next;
p1->next = p2;
ptemp = p1;
}
else
{
ptemp->next = p2->next;
ptemp = p2->next;
p2->next = ptemp->next;
ptemp->next = p2;
}
}
else
{
ptemp = p2;
p2=p2->next;
}
}
pfinished = p2;
}
}
int main(int argc, char *argv[])
{
int ndisp = 0;
struct timeval starttime, endtime, durtime;
int i = 0;

/* step0: get cmd parameter and check it */

if (argc < 2){
printf("USAGE: wordcount <filename> [Top # of results to display]\n");
exit(0);
}
char *filename = *(argv + 1);

/* step1: prepare work (e.g. open file, load file to memory ...) */

FILE *file;
if ((file = fopen(filename, "r")) == NULL){
printf("cannot open this file\n");
exit(0);
}
printf("wordcount: Running...\n");
gettimeofday(&starttime, 0);

/* step2: do word count */

char ch;
head = NULL;
while ((ch = fgetc(file)) != EOF)
{
while (ch == ' ')
ch = fgetc(file);
if (ch <= 'Z')
ch += 32;
i = 0;
q = (struct node *) malloc(sizeof(struct node));
char temp[20];
while (ch = fgetc(file) != ' ')
{
temp[i] = ch;
i++;
}
int same = 0;
for (p = head; p != NULL; p = p->next)
{
if (strcmp(temp, p->word) == 0)
{
p->count++;
same = 1;
}
}
if (same == 0)
{
strcpy(q->word, temp);
p->next = q;
}
}
gettimeofday(&endtime, 0);
GET_TIME(starttime, endtime, durtime);
printf("wordcount: completed %ld.%ld(s)\n", durtime.tv_sec, durtime.tv_usec);


printf("wordsort: running ...\n");
gettimeofday(&starttime, 0);

/* step3: sort the results */

printf_sort(head);
gettimeofday(&endtime, 0);
GET_TIME(starttime, endtime, durtime);
printf("wordsort: completed %ld.%ld(s)\n", durtime.tv_sec, durtime.tv_usec);


/* step4: print results */
ndisp = head->count;
printf("\nwordcount: results (TOP %d from %d):\n", ndisp, len);
p = head;
if (head != NULL)
{
do
{
printf("%s - %d\n", p->word, p->count);
p = p->next;
}while (p != NULL);
}
/* step5: destroy work (e.g. free memory, close file ...) */
fclose(file);
free(head);
return 0;
}

如上。程序目的是读入文件的单词并统计每一个单词的数目,然后按照数目降序打印出来。
编译可以通过,运行发生段错误,调试发现如下错误:
0x00007ffff7b664e2 in ?? () from /lib/x86_64-linux-gnu/libc.so.6
希望各位帮忙解决一下
...全文
192 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
youngyang 2012-04-06
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

LS不对吧,fgetc返回的是读到的一个字节。
[/Quote]
while (ch = fgetc(file) != ' ') 等价于while (ch = (fgetc(file) != ' '))
ch不为0或者1 能是什么

搂主用ch = fgetc(file)来接收也是不对的
ch是char类型 fgetc函数返回int类型 被截为char型
正常的字符可能会被错误的解释为EOF,或者EOF可能会被修改
wzhwisk 2012-04-05
  • 打赏
  • 举报
回复
错误的指针引用,仔细找吧,这种东西非要自己试验才有收获的
jemofh159 2012-04-04
  • 打赏
  • 举报
回复
LS不对吧,fgetc返回的是读到的一个字节。
youngyang 2012-04-04
  • 打赏
  • 举报
回复
while (ch = fgetc(file) != ' ') 这错了 ch永远是0或者1
{
temp[i] = ch;
i++;
}
眼睛有点花 你再找找吧。
ice_wind1213 2012-04-04
  • 打赏
  • 举报
回复
看了一遍 ,蒙了,坐等高手~
lybcyd 2012-04-04
  • 打赏
  • 举报
回复
能不能帮忙指出错误之处?谢谢了![Quote=引用 1 楼 的回复:]

段错误肯定是指针的引用不当造成的
[/Quote]
pengfoo 2012-04-04
  • 打赏
  • 举报
回复
段错误肯定是指针的引用不当造成的
cikerexue 2012-04-04
  • 打赏
  • 举报
回复
慢慢调试吧,设断点一步一步看,看卡在哪里
cikerexue 2012-04-04
  • 打赏
  • 举报
回复
慢慢调试吧,设断点一步一步看,看卡在哪里
AnYidan 2012-04-04
  • 打赏
  • 举报
回复
段错误一般是非法使用了内存,越界或非法解指针
jemofh159 2012-04-04
  • 打赏
  • 举报
回复
free(head) 这里错了没?你自己慢慢调吧 眼睛有点花

69,382

社区成员

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

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