Ah, I get it. What I needed to do is get the sizeof(struct list) and multiply that by length to get the new desired size. So:
Code:
memcpy(temp, friends, sizeof(struct list)*length);
seems to work.
Here is the updated code with the working expansion function:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(void) {
struct list{
char IDP [10];
char first[42];
char last[42];
char phone[42];
} *friends, *temp;
int i, j;
int k = 0;
int count;
int base = 10;
int length = 10;
char text1[10], text2[42], text3[42], text4[42];
char compare;
FILE *data;
// Determine size of data file.
data = fopen("./hw4.data", "r");
count = 0;
while (1) {
fscanf(data, "%s %s %s %s", text1, text2, text3, text4);
count++;
if (feof(data)) break;
}
// Create dynamic array of structures.
friends = (struct list *)calloc(base, sizeof(struct list));
// Populate dynamic array, resizing when needed.
rewind(data);
for (i = 0; i < count; i++) {
fscanf(data, "%s %s %s %s", friends[i].IDP, friends[i].first, friends[i].last, friends[i].phone);
if (strcmp(friends[i].IDP, "I") == 0) {
printf("Friend Added!\n");
k++;
// Resize the array every 3 additions.
if (k == 3) {
printf("Resizing list...\n");
temp = (struct list *)calloc(length+3, sizeof(struct list)); // Make temp dynamic array.
memcpy(temp, friends, sizeof(struct list)*length); // Copy friends data to temp array.
free(friends); // Free friends array.
friends = (struct list *)calloc(length+3, sizeof(struct list)); // Remake friends dynamic array.
memcpy(friends, temp, sizeof(struct list)*length); // Copy resized temp array back into friends array.
k = 0; // Reset addition counter.
length+=3; // Increment & record new length.
}
}
else if (strcmp(friends[i].IDP, "P") == 0) {
printf("Printing List!\n");
// Prints list thus far.
for (j = 0; j < i; j++) {
printf("%s %s %s\n", friends[j].first, friends[j].last, friends[j].phone);
}
}
else if (strcmp(friends[i].IDP, "D") == 0) {
printf("Deleting Friend!\n");
// Find and delete friend.
}
}
fclose(data);
// Free dynamic array.
free(friends);
return 0;
}
Here is the updated data file I'm using, for the below question.
Code:
I Ron Marsh 701-222-3333
I Dave Marsh 218-444-6666
I Radell Marsh 701-235-8133
I Brenda Marsh 701-277-5050
I Kyle Marsh 701-224-2452
I Kia Marsh 701-275-1234
I Steve Marsh 701-275-0987
I Paul Marsh 701-276-0987
I Sheila Marsh 701-277-0987
I Rick Marsh 701-278-0987
I Gregory Marsh 701-279-0987
I Keith Marsh 701-275-0987
I Shirley Marsh 701-275-0987
I Bobby Marsh 701-275-0987
P
I Junior Marsh 701-275-0987
I Peter Marsh 701-275-0987
I Hanson Marsh 701-275-0987
I Frank Marsh 701-275-0987
I Seth Marsh 701-275-0987
I David Marsh 701-275-0987
The problem with my code now is that the code exits the line 39 for loop after executing the line 56 if else statement. For example, this is what my output is:
Code:
C:\cygwin\home\Paul>a.exe
Friend Added!
Friend Added!
Friend Added!
Resizing list...
Friend Added!
Friend Added!
Friend Added!
Resizing list...
Friend Added!
Friend Added!
Friend Added!
Resizing list...
Friend Added!
Friend Added!
Friend Added!
Resizing list...
Friend Added!
Friend Added!
Printing List!
Ron Marsh 701-222-3333
Dave Marsh 218-444-6666
Radell Marsh 701-235-8133
Brenda Marsh 701-277-5050
Kyle Marsh 701-224-2452
Kia Marsh 701-275-1234
Steve Marsh 701-275-0987
Paul Marsh 701-276-0987
Sheila Marsh 701-277-0987
Rick Marsh 701-278-0987
Gregory Marsh 701-279-0987
Keith Marsh 701-275-0987
Shirley Marsh 701-275-0987
Bobby Marsh 701-275-0987
With the input file I provide, it should print the list and then continue to add to it, but it's quitting as soon as it hits the first P.