1

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;

}
Jack022
  • 867
  • 6
  • 30
  • 91
  • 5
    Please see [Why is `while ( !feof (file) )` always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) The loop could be improved as `while (fgets(one, 255, fpointer) != NULL) { printf("%s", one); }` – Weather Vane Aug 29 '18 at 14:32
  • 1
    BTW it's a good idea not to edit the code posted here, or comments won't make any sense. – Weather Vane Aug 29 '18 at 14:36
  • Got it, changed only the original one! – Jack022 Aug 29 '18 at 14:37
  • You could search each input string from the end, to find the beginning of the number, and apply `sscanf` to extract it. – Weather Vane Aug 29 '18 at 14:50
  • But where could i store each record after it? An array? – Jack022 Aug 29 '18 at 14:53
  • Maybe you can get some inspiration from the Unix command sort: https://github.com/coreutils/coreutils/blob/master/src/sort.c – Jens Mühlenhoff Aug 29 '18 at 15:10
  • For a shorter implementation have look a busybox sort: https://git.busybox.net/busybox/tree/coreutils/sort.c – Jens Mühlenhoff Aug 29 '18 at 15:12
  • I'll look into it, thanks! – Jack022 Aug 29 '18 at 17:16
  • Are you familiar with `struct`? If you save these to a file in binary, you can read them back wholesale in binary. Look at `fread()` for more details. – Code-Apprentice Aug 29 '18 at 17:54
  • 1
    This is actually quite broad task, if you don't know C or programming. You could start by creating following separate programs. 1. just read and print each line. 2. read each line and store it to a buffer allocated with `malloc` and print it from the buffer. 3. read each line, parse the number from it and print that (write a function which takes the line, and returns integer). 4. Have an array of char pointers to lines, and read all the lines to buffers allocated with `malloc`, and put them to the array, then print the lines from the array. 5. Add sorting on top of 4 by using 3. – hyde Aug 29 '18 at 17:55
  • Thank you @hyde! – Jack022 Aug 29 '18 at 21:32

1 Answers1

0

Instead of bubble you can go for Insertion Sort. Firstly, scan whole file and store each field of the line in array(you can use fscanf() for that). Like, an array user that stores values User Three;, User Two;, User One;, another one that stores Test and similarly for third field. After getting arrays full of file's content you can easily implement Insertion Sort on last field and accordingly sort all arrays and then write back the arrays to original or another file. If you need help with Insertion sort you can check this out. For string comparison use strcmp()

Hope this helps.

EDIT: to scan each line in 3 array do something like:

fscanf("%s%s%s", &user[i], &test[i], &num[i], fpointer);

run this inside a loop and aftter file reading operation you'll get all the data in the arrays.

Verma Aman
  • 149
  • 14