[comp.lang.c] Trouble spot!

thurmag@jacobs.cs.orst.edu (Gary Thurman) (04/12/91)

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@jacobs.cs.orst.edu
      +-(O)--------(O)--+       Corvallis, Oregon

gordon@osiris.cso.uiuc.edu (John Gordon) (04/12/91)

	I noticed several things about the program:

	1) When you are fscanf()'ing in the data, you need to put a & by
the integer you were reading in.  Scanf() and related functions ALWAYS
require a & before the recieving variable when reading in a single
int, float, or char.  Not for an array, though.

example: scanf("%d", &answer);

	2) You are putting the data in the file with an fwrite(), but you
are reading it in with scanf().  You really should do input and output
the same way.  I would suggest reading it in with an fread().

	3) Don't use a * with fclose.  when you say

	FILE *fp;

	close that file with

	fclose(fp);

	and don't put a * in front of it.


---
John Gordon
Internet: gordon@osiris.cso.uiuc.edu        #include <disclaimer.h>
          gordon@cerl.cecer.army.mil       #include <clever_saying.h>

id8rld06@serss0.fiu.edu (Michael N Johnston) (04/14/91)

] void find()
] {
]        [...]
]    if((fptr=fopen("maillist.txt","r"))==NULL) {
]        [...]
]    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);
]        [...]
] }

    Notice that here in find(), you are opening the file for read and doing
FORMATTED input.

] void save()
] {
]         [...]
] if((fp=fopen("maillist.txt","wb"))==NULL) {
]         [...]
] if(fwrite(&addr_info[i],sizeof(struct addr),1,fp)!=1)
]         [...]
] }

    But here in save(), you are opening the file for write in BINARY mode
and doing UNFORMATTED output.
    You need to change the open and read in find() to unformated, binary
input as you have below in load().

] void load()
] {
]         [...]
] if((fp=fopen("maillist.txt","rb"))==NULL) {
]         [...]
] if(fread(&addr_info[i],sizeof(struct addr),1,fp)!=1) {
]         [...]
] }

Mike
--
====================================================================
Michael Johnston
id8rld06@serss0.fiu.edu or
26793271x@servax.fiu.edu