#Code for EOF help
Here is some of the code from the little address program I have been working
on. Keep in mind I have only been programming in C for a few weeks.
The problem is when I call the function print_na() it prints the names in the
data file but there is a prob… Say I have ten addresses in the data file it
prints the ten, and then it just keeps printing the last name. Over and over.
It is not finding the EOF I guess. So I included the code for the part of the
program where I input the data.
/*************************************************************************/
void enter_na(void) {
struct mail_struct item;
char ans;
do
{ fflush(stdin);
printf("\n\n\n\n\nWhat is the name? ");
gets(item.name);
printf("What is the address? ");
gets(item.address);
printf("What is the city? ");
gets(item.city);
printf("What is the state? (2-letter abbreviation only) ");
gets(item.state);
getzip(item.zipcode);
strcpy(item.code, " ");
add_to_file(item);
printf("\n\nDo you want to enter another name");
printf("and address? (Y/N) ");
ans=getchar();
getchar();
} while (toupper(ans)=='Y');
fclose(fp);
return; }
/************************************************************************/
void print_na(void) {
struct mail_struct item;
int s, linectr=0;
int ret;
ret = feof(fp);
s = sizeof(struct mail_struct);
if((fp=fopen(FILENAME, "r"))==NULL)
{ err_msg("*** Read error — ensure file exists ***");
return; }
do
{ if(fread(&item, sizeof(struct mail_struct), 1, fp)!=s)
{ if(feof(fp))
{break;}
}
if (linectr>20)
{ pause_sc();
linectr=0; }
pr_data(item);
linectr+=4;
} while (ret == 0);
fclose(fp);
printf("\n- End of list -");
pause_sc();
return; }
/***********************************************************************/
void add_to_file(struct mail_struct item) {
if((fp = fopen(FILENAME, "a"))==NULL)
{ err_msg("*** Disk error — please check disk drive ***");
return; }
fwrite(&item, sizeof(item), 1, fp);
fclose(fp);
return; }
/***********************************************************************/
void pr_data(struct mail_struct item) {
printf("\nName : %-25s\n", item.name);
printf("Address: %-25s\n", item.address);
printf("City : %-12s\tState: %-2s Zipcode: %-5s\n",
item.city, item.state, item.zipcode);
return; }