I created a program supposed to take inputs from the user and write them on a text file. Now i'm making another program to read those records from the file and sort them.
So i have, for example:
User three; Test three; 30
User one; Test; 51
User two; Test two; 40
The program should sort these records according to the number at the end of it, so it should be:
User one; Test; 51
User two; Test two; 40
User three; Test three; 30
This is my current effort, basically i created a char one
variable which will store the data given from fgets
and print it. Two problems: char one
has a fixed variable (255), so if the record will be longer than 255, there will be an error.
The second problem is that i don't know how to treat each record of the file has a variable, i can print the whole file but i don't know how to compare the data according to the last number, any advice? I was looking forward to using a bubble sort for it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char one[255];
FILE * fpointer = fopen("record.txt", "r");
while (!feof (fpointer) ) {
fgets(one, 255, fpointer);
printf("%s", one);
}
fclose(fpointer);
return 0;
}