0

I want to sort employee data based on names. The sorting function works but provides strange characters in the output?? The last printf statement is the culprit I guess (bottom of the code) If someone could help, that would be appreciated. Thanks

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


typedef struct
{
   char name[25];
   char firstname[25];
   char job;
   float hrs;
   float rate;
} employee;

int main()
 {
FILE *fp = fopen("employee.dat", "r");
employee staff[30];
    int i = 0;

     if (fp == NULL){
        printf("not working\n");
        exit(1);
     }
     fscanf(fp, "%s %s %c %f %f", staff[i].name, staff[i].firstname, &staff[i].job, &staff[i].hrs, &staff[i].rate);
        while(!feof(fp))
        {
            printf("%s %s %c %4.1f %4.1f \n", staff[i].name, staff[i].firstname, staff[i].job, staff[i].hrs, staff[i].rate);
            i++;
            fscanf(fp, "%s %s %c %f %f", staff[i].name, staff[i].firstname, &staff[i].job, &staff[i].hrs, &staff[i].rate);
        }
     fclose(fp);


// qsort struct function for comparing names
int struct_cmp_by_name(const void *a, const void *b)
{
    employee *ia = (employee *)a;
    employee *ib = (employee *)b;
    return strcmp(ia->name, ib->name);
}

    int structs_len;
    structs_len = sizeof(staff) / sizeof(employee);

// sort on names
qsort(staff, structs_len, sizeof(employee), struct_cmp_by_name);

//output with strange charaters???
for(i=0; i<structs_len; i++){
  printf("%s %s %c %4.1f %4.1f \n", staff[i].name, staff[i].firstname, staff[i].job, staff[i].hrs, staff[i].rate); 
}
     return(0);
}


I am expecting a regular output of the printf statement. The first printf works fine but the one after the qsort provides strange characters instead??

stuski
  • 199
  • 1
  • 11
  • 1
    Please take some time to read [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Some programmer dude Feb 02 '19 at 15:09
  • 1
    And what compiler are you using that allows nested functions? That's not part of standard C. – Some programmer dude Feb 02 '19 at 15:10
  • You allow space for 30 `employee` structures and sort and print the whole array, but are you actually inputting that many? Your input stage allows you to stop at fewer. – John Bollinger Feb 02 '19 at 15:12
  • The compiler is QT Creator – stuski Feb 02 '19 at 15:12
  • I am inputting less....15 for this example – stuski Feb 02 '19 at 15:13
  • You must sort (and later print) only the structures you have actually read. I'm unsure whether that's your only issue, but accessing data that have not been initialized or set is erroneous, and it very well could result in odd characters being printed in this case. – John Bollinger Feb 02 '19 at 15:16

1 Answers1

2

The most likely culprit of your problem is that you sort the whole array, even when maybe not all elements are initialized.

If the file contains less than the 30 elements you have for the array, parts of the array will be uninitialized with indeterminate contents (which may sometimes seem random or like "garbage"). You should not use those when sorting, only sort the data you actually have read from the file.

You have the number of valid and initialized elements in the array in the variable i which you should use instead:

qsort(staff, i, sizeof(employee), struct_cmp_by_name);

You have the same problem when printing the data: You print the whole array, including the uninitialized parts.

I suggest you create a new variable for the number of valid elements, suitable named, instead of the generic i that you now use.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Upvoted, but Indeterminate != seemingly random. The former *may* manifest that way, but it is not safe to *expect* such a manifestation. We've had questions about this very thing before, ala "My variable is uninitialized, so why do I always get 0 when I print it?" and "Can I use an uninitialized variable as a cheap random number?" – John Bollinger Feb 02 '19 at 15:19
  • 1
    @JohnBollinger Rephrased. Thanks. – Some programmer dude Feb 02 '19 at 15:23