HEEY EVERYONE ,hope you're doing well
my teacher assigned us a homework wich is to create a program with C;
this program takes as input a text file with some data in it .every line of the text file contains the ID of an employee and it's name and salary e.g
1 katy oath 100.34
22 bob jazy 3343.00
4 jonathan sayf 2000.00
and so on
then sort them according to salary o the employee with the highest salary shold be at the firt ,so it should be like this :
22 bob jazy 3343.00
4 jonathan sayf 2000.00
1 katy oath 100.34
BUT without using structures
ummm this is what i've done so far ..

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{

    return 0;
}
void tri_file (char *souece_file_name)
{
    // array for employee names,salary and id
    char File_line_tab[];
    //creating pointers for Source file and final file
    FILE *source_file,*destination_file;
    //cheking if source file does not exit
    source_file = fopen(souece_file_name ,"r");
    if(source_file == NULL)
    {
        printf("Error opening source file");
        exit(33);
    }
        destination_file = fopen("Tri_data_file.txt" ,"w");
        if(destination_file == NULL)
            {
                printf("Error f1");
                exit(33);
            }
                // reading data and sort source_file data according to salary
                while(! feof(source_file))
                {
                    // the sorting code here
                }

                fclose(source_file);
                fclose(destination_file);
}

actually im still strugling with how to extract from every line the ID numbers and the names and the salary and store every one in a separate array
PLEASE give me a code that extract and sort according to salary

Dani AI

Generated

For — a compact, robust way to do this without using structs is to keep three parallel arrays (id, name, salary), parse each input line by isolating the last token as the salary (so multi-word names are preserved), take the first token as the id, and store the rest as the name. The example below reads the whole file, sorts entries by salary (highest first) and writes a new file "Tri_data_file.txt". As suggested, keeping the file I/O in the exercise is the point; this implementation shows a practical, safe parsing approach and simple in-memory sorting.

Notes before the code:

  • Splits on the last whitespace to extract salary, and on the first whitespace to extract id; this handles names with spaces.
  • Uses dynamic arrays (realloc) and a selection sort (O(n^2)) which is fine for classroom-sized files. For large files, use qsort on an index array or store records externally.
  • Trims blank lines and malformed lines are skipped quietly; adjust error handling if strict validation is needed.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

static char *xstrdup(const char *s) {
    size_t n = strlen(s) + 1;
    char *r = malloc(n);
    if (!r) { perror("malloc"); exit(EXIT_FAILURE); }
    memcpy(r, s, n);
    return r;
}

static void trim_trailing(char *s) {
    size_t n = strlen(s);
    while (n && isspace((unsigned char)s[n-1])) s[--n] = '\0';
}

void tri_file(const char *src) {
    FILE *in = fopen(src, "r");
    if (!in) { perror("fopen"); exit(EXIT_FAILURE); }

    size_t cap = 64, n = 0;
    int *ids = malloc(cap * sizeof *ids);
    double *sals = malloc(cap * sizeof *sals);
    char **names = malloc(cap * sizeof *names);
    if (!ids || !sals || !names) { perror("malloc"); exit(EXIT_FAILURE); }

    char line[2048];
    while (fgets(line, sizeof line, in)) {
        trim_trailing(line);
        char *p = line;
        while (*p && isspace((unsigned char)*p)) p++;
        if (*p == '\0') continue;

        char *last = line + strlen(line) - 1;
        while (last >= line && isspace((unsigned char)*last)) *last-- = '\0';
        char *sep = last;
        while (sep >= line && !isspace((unsigned char)*sep)) sep--;
        if (sep < line) continue;
        char *salary_str = sep + 1;
        *sep = '\0';

        char *endptr;
        double salary = strtod(salary_str, &endptr);
        if (salary_str == endptr) continue;

        char *id_str = line;
        while (*id_str && isspace((unsigned char)*id_str)) id_str++;
        char *space_after_id = id_str;
        while (*space_after_id && !isspace((unsigned char)*space_after_id)) space_after_id++;
        if (*space_after_id == '\0') continue;
        *space_after_id = '\0';
        int id = (int)strtol(id_str, &endptr, 10);
        if (id_str == endptr) continue;

        char *name = space_after_id + 1;
        while (*name && isspace((unsigned char)*name)) name++;
        trim_trailing(name);

        if (n == cap) {
            cap *= 2;
            int *tids = realloc(ids, cap * sizeof *ids);
            double *tsals = realloc(sals, cap * sizeof *sals);
            char **tnames = realloc(names, cap * sizeof *names);
            if (!tids || !tsals || !tnames) { perror("realloc"); exit(EXIT_FAILURE); }
            ids = tids; sals = tsals; names = tnames;
        }
        ids[n] = id;
        sals[n] = salary;
        names[n] = xstrdup(name);
        n++;
    }
    fclose(in);

    for (size_t i = 0; i + 1 < n; ++i) {
        size_t imax = i;
        for (size_t j = i + 1; j < n; ++j)
            if (sals[j] > sals[imax]) imax = j;
        if (imax != i) {
            int ti = ids[i]; ids[i] = ids[imax]; ids[imax] = ti;
            double ts = sals[i]; sals[i] = sals[imax]; sals[imax] = ts;
            char *tn = names[i]; names[i] = names[imax]; names[imax] = tn;
        }
    }

    FILE *out = fopen("Tri_data_file.txt", "w");
    if (!out) { perror("fopen"); exit(EXIT_FAILURE); }
    for (size_t i = 0; i < n; ++i) fprintf(out, "%d %s %.2f\n", ids[i], names[i], sals[i]);
    fclose(out);

    for (size_t i = 0; i < n; ++i) free(names[i]);
    free(names); free(ids); free(sals);
}

int main(int argc, char **argv) {
    if (argc < 2) { fprintf(stderr, "Usage: %s input.txt\n", argv[0]); return 2; }
    tri_file(argv[1]);
    return 0;
}

Troubleshooting tips: increase the line buffer if names can exceed 2K, add logging for skipped lines to locate malformed input, and switch to qsort + an index array if performance becomes important. Using a struct would make the code cleaner for production, but the parallel-array approach above meets the "no structs" constraint.

So, basically, you want us to do everything except the actual file I/O? What, exactly, will you learn if we do that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.