my C class is having us read from a file with an unknown number of entries but a known format, which goes like this:
firstname, lastname
address line 1
address line 2
zip code
So we're dynamically allocating an array of pointers to structs, and I made a function getData which will use gets to read line by line and store everything appropriately. My biggest problem is how to make it so that it stops collecting data when the file has nothing left to read. I have crudely come up with while((strcpy(ptr[i].name, buffer))!=EOF)
which I'm sure makes no sense but it seemed to somewhat work, especially because it correctly prints out almost every entry. Here is the code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "address.h"
struct AddressBook {
char *name[10];
char *address[50];
int zip;
};
typedef struct AddressBook adds;
void getData(adds *ptr);
int main() {
//adds *ptrArr[50];
int size = 50;
adds *ptrs = (adds*)calloc(size, sizeof(adds*)); //array of 50 ptrs allocated dynamically
if (ptrs == NULL) {
printf("Bad memory allocation...exiting.");
exit(0);
}
else
printf("Allocation successful...\n");
getData(ptrs);
system("pause");
return 0;
}
void getData(adds *ptr) {
printf("Gathering data...\n");
char buffer[50];
int i = 0;
gets(buffer);
while((strcpy(ptr[i].name, buffer))!=EOF) {
if (i > 0) {
gets(buffer);
strcpy(ptr[i].name, buffer);
}
/*gets(buffer);
strcpy(ptr[i].name, buffer);*/
gets(buffer);
strcpy(ptr[i].address, buffer);
gets(buffer);
strcat(ptr[i].address, " ");
strcat(ptr[i].address, buffer);
gets(buffer);
ptr[i].zip = atoi(buffer);
printf("Printing data for line %d...\n", i);
printf("name is: %s\naddress is: %s\nzip is: %d\n", ptr[i].name, ptr[i].address, ptr[i].zip);
printf("\n");
i++;
}
}
Two problems are happening: 1. When I print, it will print up to 50 entries, which is just the amount of space I asked for but the actual number of entries is far less.
2.It prints our every entry correctly except for line 2. (or subscript 1) here is the input if you're curious: https://pastebin.com/Ph5wzFeF
here is the output for the first 3 entries:
Printing data for line 0...
name is: A1, A2
address is: 20294 Lorenzana Dr Woodland Hills, CA
zip is: 91364
Printing data for line 1...
name is: B1, name is: B1, name is: B1, name is: B1, name is: B1, name is: B1, nam
address is: 1, name is: B1, name is: B1, name is: B1, nam
address is: 1, name is: B1, name is: B1, name is: B1, nam
address is: 1, name is: B1, name is: B1, name is: B1, nam
address is: 1, name is: B1, name is: B1,
zip is: 94023
Printing data for line 2...
name is: C1, C2
address is: 5142 Dumont Pl Azusa, CA
zip is: 91112
Debugging has been a pain because I can't use i/o redirection and the visual studio debugger at the same time. If anyone has any suggestions as to how I can debug I would appreciate it! But otherwise I would like some feedback on what i've got.