Trouble spot!
Gary Thurman
thurmag at jacobs.cs.orst.edu
Fri Apr 12 06:11:16 AEST 1991
I am in the process of learning Turbo-C, and have learned most of what I know
through reading books. I have finally reached a point for which I need help
from people that know what their doing! :-) I am trying to re-call data from
a simple mailing list database, just by giving a persons name. I have a
feeling that I'm not setting up the function "Find()" the right way. Any
help would sure be appreciated. Thanks.
Here is the program:
/* A simple mailing list */
#include <stdio.h>
#include <ctype.h>
#define SIZE 100
struct addr {
char name[40];
char street[40];
char city[30];
char state[3];
char zip[10];
} addr_info[SIZE];
void enter(), init_list(), display(), save(), load(), find();
main()
{
char choice;
init_list();
for(;;) {
choice = menu();
switch(choice) {
case 'e': enter();
break;
case 'd': display();
break;
case 'f': find();
break;
case 's': save();
break;
case 'l': load();
break;
case 'q': exit(1);
}
}
}
/* initialize the addr_info array */
void init_list()
{
register int t;
for(t=0; t<SIZE; t++) *addr_info[t].name='\0';
/* a zero length name signifies empty */
}
/* get a menu selection */
menu()
{
char s[80];
do {
printf("\t\t\t(E)nter\n");
printf("\t\t\t(D)isplay\n");
printf("\t\t\t(F)ind\n");
printf("\t\t\t(L)oad\n");
printf("\t\t\t(S)ave\n");
printf("\t\t\t(Q)uit\n");
printf("\t\t\tchoose one: ");
gets(s);
} while(!strrchr("edflsq", tolower(*s)));
return tolower(*s);
}
/* put names into addr_info */
void enter()
{
register int i;
for(i=0; i<SIZE; i++)
if(!*addr_info[i].name) break;
if(i==SIZE) {
printf("addr_info full\n");
return;
}
printf("Name: ");
gets(addr_info[i].name);
printf("Street: ");
gets(addr_info[i].street);
printf("City: ");
gets(addr_info[i].city);
printf("State: ");
gets(addr_info[i].state);
printf("Zip: ");
gets(addr_info[i].zip);
}
/* display the addr_info */
void display()
{
register int t;
for(t=0; t<SIZE; t++) {
if(*addr_info[t].name) {
printf("%s\n", addr_info[t].name);
printf("%s\n", addr_info[t].street);
printf("%s\n", addr_info[t].city);
printf("%s\n", addr_info[t].state);
printf("%s\n", addr_info[t].zip);
}
}
}
/* ************************* Trouble Area ************************ */
/* reads formated data from database */
void find()
{
FILE *fptr; /* declare ptr to FILE */
char name[40]; /* maillist name */
int *addr_info
/* open it to read */
if((fptr=fopen("maillist.txt","r"))==NULL) {
printf("cannot open file\n");
return;
}
printf("Name? ");
gets(name);
/* look for info */
while(!feof(fptr)) {
fscanf(fptr,"%s %s %s %s %d", addr_info[t].name, addr_info[t].street, addr_info[t].city, addr_info[t].state, addr_info[t].zip);
if(!strcmp(addr_info[t].name)) {
printf("%s %s %s %s %d\n", addr_info[t].name, addr_info[t].street, addr_info[t].city, addr_info[t].state, addr_info[t].zip);
break;
}
}
fclose(*fptr);
}
/* ***********This is the end of the find() function********** */
/* save the list */
void save()
{
FILE *fp;
register int i;
if((fp=fopen("maillist.txt","wb"))==NULL) {
printf("cannot open file\n");
return;
}
for(i=0; i<SIZE; i++)
if(*addr_info[i].name)
if(fwrite(&addr_info[i],sizeof(struct addr),1,fp)!=1)
printf("file write error\n");
fclose(fp);
}
/* load the file */
void load()
{
FILE *fp;
register int i;
if((fp=fopen("maillist.txt","rb"))==NULL) {
printf("cannot open file\n");
return;
}
init_list();
for(i=0; i<SIZE; i++)
if(fread(&addr_info[i],sizeof(struct addr),1,fp)!=1) {
if(feof (fp)) {
fclose(fp);
return;
}
printf("file read error\n");
}
}
--
_**_ "We came...We saw...We kicked some ASH!"
/____|-IIIIIIIIIIII Gary Thurman ------ FIREFIGHTER/EMT 2
>| 132 |-----------\ Email: thurmag at jacobs.cs.orst.edu
+-(O)--------(O)--+ Corvallis, Oregon
More information about the Comp.lang.c
mailing list