./a.out *.txt
単語の使用頻度をカウントして最後に表示したいのですがエラーになってしまいます・・・
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
typedef struct token_checker{
char *token;
int count;
}TOKEN_CHECKER;
int main(int argc,char* argv[]){
FILE * fp;
char buffer[1024];
int numword=0;
char * delimiter = " .,;:!?"; //除外する文字
char * s;
TOKEN_CHECKER *t;
int i,k;
int find;
t=(TOKEN_CHECKER*)malloc(sizeof(TOKEN_CHECKER)*24);
if(argc < 2){
printf("Parameter error.\n");
exit(1);
}
for(k=1;k<argc;k++){
if((fp=fopen(argv[k],"r"))==NULL){
printf("File open error.\n");
exit(1);
}
while( fgets(buffer,sizeof(buffer),fp)!=NULL){
buffer[strlen(buffer)-1]='\0';
s = strtok(buffer,delimiter);
if(s != NULL){
find=0;
for(i=1;i<=numword;i++){
if(strcmp(s,t[i].token)==0){
t[i].count++;
find=1;
}
}
if(find==0){
numword++;
t[numword].token=(char*)malloc(strlen(s)+1);
strcpy(t[numword].token,s);
t[numword].count=1;
}
}
while((s=strtok(NULL,delimiter)) != NULL){
find=0;
for(i=1;i<=numword;i++){
if(strcmp(s,t[i].token)==0){
t[i].count++;
find=1;
}
}
if(find==0){
numword++;
t[numword].token=(char*)malloc(strlen(s)+1);
strcpy(t[numword].token,s);
t[numword].count=1;
}
}
}
}
free(t);
fclose(fp);
for(i=1;i<=numword;i++){
printf("%8s %d回\n",t[i].token,t[i].count);
free(t[i].token);
}
exit(0);
}