CompuServe Thread

#Code for EOF help

3 messages in this thread
#41877From: BartJul 16, 1994 10:31 PM
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; }
#41882From: Brian BartlettJul 17, 1994 12:42 AM
Bart, I think your problem has to do with the test on your DO WHILE loop. If you look, you are telling it to perform the DO…WHILE so long as ret==0. But if you look at your loop, you are not setting the value anywhere within the loop. Instead, you set it as part of your initialization code. I'd drop the initialization line: ret = feof(fp); and change that whole block to something like: do { if(fread(&item, sizeof(struct mail_struct), 1, fp)!=s) { ret = feof(fp); if (ret) break; } if (linectr>20) ….. and so on. A common mistake. I've made it half a thousand times myself. Make sure your looping variable is going to change state somewhere inside the loop. "You do trust me, don't you? Of course you do." — "To Play the King" (BBC)
#41883From: Eulogio (DJ) GarciaJul 17, 1994 2:19 AM
Bart, it appears to me there's at least a couple of problems: 1. You're checking for fread to return the number of bytes in mail_struct, but it actually returns the number of blocks read (mail_structs in this case) which should be 1 or 0 in this case. 2. You're using the variable ret as a loop control, but you never set it inside the loop, only once at the beginning of the subroutine, when fp is undefined. 3. You should also show the definition of mail_struct, otherwise we don't have a full picture. At this point I can't really determine why the feof isn't breaking out of the do-while, since it should be executing on every read, depending on mail_struct. You may want to make your indentation and blocking format more uniform. It would make the source easier to read and detect syntax problems. Here's one example: 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; } DJ My Sweet Amiga /-\/-\ As personable as Cindy, \ — / As kicking as Arnold …