|
|
#include<stdio.h>
#include<string.h>
#include<glib.h>
struct words{
gchar* word;
unsigned int wf;
};
struct words ws[100];
guint size;
unsigned int i=0;
int n,m;
struct words temp1;
void getKeyword(gpointer key,gpointer value,gpointer user_data)
{
g_print("%d%s",*((int*)value),key);
}
main(void){
FILE*fp;
char* keyword;
GHashTable* ghTable;
long* f;
int*temp;
int c;
//读文件数据到哈希表
ghTable=g_hash_table_new(g_str_hash,g_str_equal);
fp=fopen("log.txt","r");
while((fgets(keyword,255,fp))!=NULL){
//g_print("%s",keyword);
f=(long*)g_hash_table_lookup(ghTable,keyword);
if(f==NULL){
temp=(int *)malloc(sizeof(int));
*temp=1;
g_hash_table_insert(ghTable,keyword,temp);
}
else{
(*f)++;
}
}
//输出
哈希表内数据
g_hash_table_foreach_steal(ghTable,(GHFunc)getKeyword,NULL);
}
为什么最后输出的都是哈希表内同一个键,但是值还是不同键的对应值。
而我想输出的是所有表内的键值对 |
|