69,373
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#define KEYSIZE 100
#define MAXSIZE 1024
typedef struct{
char *key;
char **trans;
int n;
}word_t;
word_t **p;
int pos = 0;
int alpha = 0;
void insert_array(word_t *,int);
int read_txtfile(void)
{
FILE *fp;
p = (word_t **)malloc(sizeof(word_t *));
printf("sizeof word_t = %d\n",sizeof(word_t));//我瞎编的结构体,windows32位运行结果是12,linux 64位运行结果是24
printf("sizeof word_t* = %d\n",sizeof(word_t *));//windows32位运行结果是4,linux 64位运行结果是8
printf("sizeof char = %d\n",sizeof(char));//windows32位运行结果是1,linux 64位运行结果当然也是1
printf("sizeof char* = %d\n",sizeof(char *));//windows32位运行结果是4,linux 64位运行结果是8
printf("sizeof char ** = %d\n",sizeof(char **));//windows32位运行结果是4,linux 64位运行结果是8
fp = fopen("dict.txt", "r"); //打开文件dict.txt,只读
if(!fp)
{
puts("数据库文件不存在!");
return 0;
}
word_t *tmp = (word_t *)malloc(sizeof(word_t));
tmp->key = (char *)malloc(KEYSIZE+1);
tmp->trans = (char **)malloc(sizeof(char *) * 1);
tmp->trans[0] = (char *)malloc(MAXSIZE * 1);
while(fgets(tmp->key,KEYSIZE,fp))
{
fgets(tmp->trans[0],MAXSIZE,fp); //单词解释中@前后没有换行符,所以将整个解释读入了trans[0]中
insert_array(tmp,pos);
pos++;
}
free(tmp->trans[0]);
free(tmp->trans);
free(tmp->key);
free(tmp);
fclose(fp);
return 1;
}
void insert_array(word_t *tmp,int pos)
{
extern word_t **p;
int count = 0;
//word_t **temp_ads = NULL;
if(*(tmp->key + strlen(tmp->key) - 1) == '\n')
*(tmp->key + strlen(tmp->key) - 1) = '\0';
if(*(tmp->trans[0] + strlen(tmp->trans[0]) - 1) == '\n')
*(tmp->trans[0] + strlen(tmp->trans[0]) - 1) = '\0';
p = (word_t **)realloc(p, sizeof(word_t*) * (pos + 1));
p[pos] = (word_t *)malloc(sizeof(word_t));
p[pos]->key = (char *)malloc(strlen(tmp->key) + 1);
strcpy(p[pos]->key,tmp->key + 1); //读入字符串第一位是#,所以从key+1开始拷贝
if(tolower(*p[pos]->key) >= 'a' && tolower(*p[pos]->key) <= 'z')
alpha++;
char str[1024];
strcpy(str,tmp->trans[0]);
char *token = strtok(str,";"); //将文本中的Trans过滤掉
p[pos]->trans = NULL;
while((token = strtok(NULL,"@"))) //将@以前Trans以后的内容放入token中
{
p[pos]->trans = (char **)realloc(p[pos]->trans, sizeof(char *) * (count + 1)); //调整指针的大小,每个指针大小为4
p[pos]->trans[count] = (char *)malloc(strlen(token) + 1);
strcpy(p[pos]->trans[count],token);
count++;
}
p[pos]->n = count;
}
int main()
{
read_txtfile();
return 0;
}