I have this text file with various records, each record has 4 fields, 3 strings of words and one number, for example
field one; field two; field three; 1
field one; field two; field three; 2
I need to sort those records according to the last number through an insertion sort algorithm, so the second record should be the first one in the list, since 2>1. To do that, i first need to store each record before comparing them, i'm having some problems with that.
I got this until now:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 100
struct Element
{
char one[100];
char two[100];
char three[100];
int st;
};
int main() {
int i;
struct Element elements[MAXLEN];
FILE * fpointer = fopen("clients.txt", "r");
for (i = 0; i < MAXLEN; i++) {
struct Element *e = elements+i; // pointer on ith element
fscanf(fpointer, "%99s%99s%99s%d", e->one, e->two, e->three, &e->st);
}
printf("%s%s%s%d", elements->one, elements->two, elements->three, elements->&st);
fclose(fpointer);
return 0;
}
My current problems:
1) this current program is not good for saving string of words. It's good when the record is like: string; string; string; 1 but it won't work with phrases (like in the first example).
2) I don't really understand the structure of this: i know that i'm saving each record in my array of data structures but i'm having some problems printing each record. Let's say i want to print the second record, how can i do that? In my current work i printed one, two, three and st but this way, it will only print the first record.