/*

============================================================================

Name        : Hash2.c

Author      : SeYUN

Version     :

Copyright   : Your copyright notice

Description : Hello World in C, Ansi-style

============================================================================

*/

#include <stdio.h>

#include <search.h>

#include <string.h>

struct info {     /* This is the info stored in the table */

int age, room;     /* other than the key. */

};

#define NUM_EMPL     5000    /* # of elements in search table. */

int

main(void)

{

FILE *fp;

char string_space[NUM_EMPL*20]; /* Space to store strings. */

struct info info_space[NUM_EMPL]; /* Space to store employee info. */

char *str_ptr = string_space; /* Next space in string_space. */

struct info *info_ptr = info_space; /* Next space in info_space. */

ENTRY item;

ENTRY *found_item; /* Name to look for in table. */

char name_to_find[30];

int i = 0;

/* Create table; no error checking is performed. */

(void) hcreate(NUM_EMPL);

if ( fp = fopen("./input.dat", "r")) {

if(fp == NULL) {

printf("파일 open fail\n");

exit(1);

}

while (0 < fscanf(fp, "%s%d%d", str_ptr, &info_ptr->age, &info_ptr->room) && i++ < NUM_EMPL) {

/* Put information in structure, and structure in item. */

item.key = str_ptr;

item.data = info_ptr;

str_ptr += strlen(str_ptr) + 1;

info_ptr++;

/* Put item into table. */

(void) hsearch(item, ENTER);

}

}

/* Access table. */

item.key = name_to_find;

//strncpy(item.key, "A000000002", 10);

printf("find key? ");

while (scanf("%s", item.key) != EOF) {

if ((found_item = hsearch(item, FIND)) != NULL) {

/* If item is in the table. */

(void)printf("found %s, age = %d, room = %d\n",

found_item->key,

((struct info *)found_item->data)->age,

((struct info *)found_item->data)->room);

} else

(void)printf("no such employee %s\n", name_to_find);

}

return 0;

}



+ Recent posts