Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(void) {
// Build friends structure.
struct list{
char IDP[10];
char first[42];
char last[42];
char phone[42];
} *friends, *temp;
// Define variables.
int j, l, m, n, a, b, c;
int i = 0;
int base = 10;
int length = 10;
char text1[10], text2[42], text3[42], text4[42], test[136];
FILE *data;
// Open data file.
data = fopen("./hw4.data", "r");
// Create dynamic array of structures.
friends = (struct list *)calloc(base, sizeof(struct list));
// Populate dynamic array, deleting, adding and resizing when needed.
while (fgets(test, sizeof(test), data) != NULL) {
sscanf(test, "%s %s %s %s", text1, text2, text3, text4);
//printf("Size of length: %d\n", length); // For testing.
//printf("Size of array: %d\n", i); // For testing.
if (strcmp(text1, "I") == 0) {
printf("Friend added!\n");
strncpy(friends[i].IDP, text1, sizeof(text2));
strncpy(friends[i].first, text2, sizeof(text2));
strncpy(friends[i].last, text3, sizeof(text2));
strncpy(friends[i].phone, text4, sizeof(text2));
// Sort dynamic array thus far alphabetically.
temp = (struct list *)calloc(length, sizeof(struct list));
for (n = i; n >= 0; n--) {
for (j = n; j < i; j++) {
if(strcmp(friends[j].first, friends[j+1].first) > 0) {
*temp = friends[j];
friends[j] = friends[j+1];
friends[j+1] = *temp;
}
}
}
free(temp);
i++;
// Resize the array every 3 additions past the original length of 10.
if (length - i == 0) {
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.
length+=3; // Increment and record new length.
}
}
else if (strcmp(text1, "P") == 0) {
printf("Printing List!\n");
printf("*----------------------------*\n");
// Prints entire dynamic array thus far.
for (j = 0; j < i; j++) {
printf("%s %s %s\n", friends[j].first, friends[j].last, friends[j].phone);
}
printf("*----------------------------*\n");
}
else if (strcmp(text1, "D") == 0) {
printf("Finding friend to delete...\n");
// Find and delete friend.
for (l = 0; l < i; l++) {
if (strcmp(text2, friends[l].first) == 0 &&
strcmp(text3, friends[l].last) == 0 &&
strcmp(text4, friends[l].phone) == 0)
{
printf("Found! Deleting friend and decreasing list size...\n");
// Deletes friend from dynamic array.
for (m = l; m < i; m++) {
strncpy(friends[m].IDP, friends[m+1].IDP, sizeof(text1));
strncpy(friends[m].first, friends[m+1].first, sizeof(text2));
strncpy(friends[m].last, friends[m+1].last, sizeof(text2));
strncpy(friends[m].phone, friends[m+1].phone, sizeof(text2));
}
break;
}
}
i--;
length--;
// Same as above resize function only moves the entire friends array into a new array of -1 size (decreases array size).
temp = (struct list *)calloc(length, sizeof(struct list));
memcpy(temp, friends, sizeof(struct list)*length);
free(friends);
friends = (struct list *)calloc(length, sizeof(struct list));
memcpy(friends, temp, sizeof(struct list)*length);
free(temp);
}
}
// Close data file.
fclose(data);
// Free dynamic array.
free(friends);
return 0;
}
Thanks everyone for the help! I'm sorry I ask so many questions, I'm still a beginner at C (or any type of prorgramming) and I still don't know a lot of the methods or commands.