#include<stdio.h>
#include<stdlib.h>
#include <ctype.h>
#include <string.h>
#define WORD_LEN_MAX 256
typedef struct node{
char word[WORD_LEN_MAX];
int count;
int ASCII_word[WORD_LEN_MAX];
struct node *next;
}node_t;
node_t *list = NULL;
node_t *create_node(char *word){
node_t *node;
node = malloc(sizeof(node_t));
if(node == NULL){
printf("malloc eror\n");
}
strncpy(node->word, word, sizeof(node->word));
node->count = 1;
return node;
}
void enqueue_noode(node_t *node){
node_t *tmp = list;
while (tmp->next == NULL){
tmp = tmp->next;
}
tmp->next = node;
node->next = NULL;
}
node_t *search_node(char *word){
node_t *tmp = list;
while(strcmp(tmp->word,word) != 0){
if(tmp == NULL){
break;
}
tmp = tmp->next;
}
return tmp;
}
void sort_node_by_word(void){
node_t *tmp = list;
int i;
while(tmp != NULL){
for(i = 0; i < strlen(tmp->word); i++){
tmp->ASCII_word[i] = tmp->word[i];
}
tmp = tmp->next;
}
node_t prev;
node_t *keep = NULL;
prev.next = list;
while (prev.next != NULL){
node_t *premax = &prev;
node_t *max = NULL;
node_t *node;
for(node = &prev; node->next != NULL; node = node->next){
int j = 0;
i = 0;
while(j != 0)
{
if(premax->next->ASCII_word[i] < node->next->ASCII_word[i]){
premax = node;
j = 1;
}else if(premax->next->ASCII_word[i] == node->next->ASCII_word[i]){
i++;
}else{
break;
}
}
}
max = premax->next;
premax->next = max->next;
max->next = keep;
keep = max;
}
list = keep;
}
void sort_node_by_count(void){
node_t prev;
node_t *keep = NULL;
prev.next = list;
while (prev.next != NULL) {
node_t *premax = &prev;
node_t *max;
node_t *node;
for (node = &prev; node->next != NULL; node = node->next) {
if (premax->next->count < node->next->count) {
premax = node;
}
}
max = premax->next;
premax->next = max->next;
max->next = keep;
keep = max;
}
list = keep;
}
void print_list(void){
node_t *tmp = list;
while(tmp != NULL){
printf("%s %d\n",tmp->word,tmp->count);
tmp = tmp->next;
}
}
int main(int argv,char *argc[]){
node_t *node;
//node_t *tmp = NULL;
FILE *fp;
if(argv != 3){
printf("short or over comand\n");
exit(1);
}
fp = fopen(argc[2],"r");
if(fp == NULL){
printf("file open error\n");
exit(1);
}
int n;
char buf[WORD_LEN_MAX];
buf[0] = '\0';
char c;
int flag;
while ((c = fgetc(fp)) != EOF){
if('A' <= c && c <= 'Z' ){c = c - ('A' - 'a');}
if(isalpha(c) != 0){
n = strlen(buf);
buf[n] = c;
buf[n+1] = '\0';
flag = 1;
}else if(flag == 1){
node = search_node(buf);
if(node == NULL){
printf("NULL ");
}else{
printf("%s ",node->word);
}
/*if(node == NULL){
node = create_node(buf);
node->next = tmp;
tmp = node;
}else{
node->count++;
}*/
printf("%s\n",buf);
buf[0] = '\0';
flag = 0;
}
}
fclose(fp);
if(strcmp(argc[1],"-o") == 0){
print_list();
}else if(strcmp(argc[1],"-c") == 0){
//sort_node_by_count();
print_list();
}else if(strcmp(argc[1],"-w") == 0){
//sort_node_by_word();
print_list();
}else{
printf("comand error\n");
}
return 0;
}
詳しい原因を探すため145~150を有効にしGDBデバッガを用いて実行した((gdb)r -o -test.txt)結果、以下のようなエラーを受け取りました。
Program received signal SIGSEGV, Segmentation fault.
__strcmp_see42( )at../sysdeps/x84_64/multiarch/strcmp-see42.S:164
164 movdqu (%rdi), %xmm1
これは164行目のfclose(fp)でセグメンテーションエラーが出ているということでしょうか?
それともstrcmpの使い方にエラーが出ているのでしょうか?
再度main内145から150のsearch_nodeに関する部分を無効にしたうえで同様にGDBデバッガ内で実行した結果エラーメッセージは何も出ず、期待した結果とexited normaly(正常終了ですよね?)が出力されました。
やはりsearch_nodeの周辺がおかしいのでしょうか?
自分なりにデバッグを色々試してみたつもりだったのですが、原因はもちろん場所すら分からなくなり手詰まり状態です。助けて欲しいです。