0

I have an array of struct,that I read from the text file.I wrote a sort function,that has to sort this array by the name field in alphabet order(A-Z),but it doesnt work,i dont know what to do,plz,can you tell me my mistake(code compiles,but it doesnt sort anything).

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

int my_compare(const void*,const void*);
typedef struct {
    char name[100];
    char surname[100];
    char sname[100];
    char posada[100];
    char buff[100];
    int staz;
    char buff2[100];
    int oklad;
} test;
        ;
int main()
{
    test mass[7];
    /*struct test{
        char name[100];
        char surname[100];
        char sname[100];
        char posada[100];
        char buff[100];
        int staz;
        char buff2[100];
        int oklad;
    }mass[8];
    */
    FILE *fo;
    if((fo=fopen("C:\\Users\\andyb\\Desktop\\test2.txt","r"))==NULL)
        printf("error");
    for(int i=0;i<7;i++)
    {
        fgets(mass[i].name,50,fo);
        fgets(mass[i].surname,50,fo);
        fgets(mass[i].sname,50,fo);
        fgets(mass[i].posada,50,fo);
        fgets(mass[i].buff,50,fo);
        mass[i].staz=atoi(mass[i].buff);
        fgets(mass[i].buff2,50,fo);
        mass[i].oklad=atoi(mass[i].buff2);
    }
    fclose(fo);
    for(int i=0;i<7;i++)
    {
        printf("%s%s%s%s%d\n%d\n",mass[i].name,mass[i].surname,mass[i].sname,mass[i].posada,mass[i].staz,mass[i].oklad);
    }
    qsort( mass, sizeof(mass)/sizeof(mass[0]), sizeof(mass[0]), my_compare );
    for(int i=0;i<7;i++)
    {
        printf("%s%s%s%s%d\n%d\n",mass[i].name,mass[i].surname,mass[i].sname,mass[i].posada,mass[i].staz,mass[i].oklad);
    }

    return 0;
}

int my_compare(const void *a,const void *b)
{
    const test *pa = (const test *)a;
    const test *pb = (const test *)b;

    return strcmp( pa->name, pb->name );
}
8
  • 3
    "it doesn't work" → what does it do? Commented Oct 6, 2016 at 5:49
  • It does not sort -> It does not do something at all? How is the text file formatted? Commented Oct 6, 2016 at 5:50
  • 2
    Is there any particular reason why you are not initializing mass[0] ? Commented Oct 6, 2016 at 5:52
  • 2
    Because you aren't initializing mass[0], but you are definitely trying to sort with it. :) Commented Oct 6, 2016 at 5:53
  • 1
    Your last loop still iterates out of bounds. Do you mind show us a sample of your input file too? Commented Oct 6, 2016 at 6:06

2 Answers 2

3

Your for loops look incorrect:

Instead of this:

for(int i=1;i<8;i++)

You should be saying this:

for(int i=0;i<8;i++)

Otherwise, mass[0] is going to contain garbage data and all sorts of undefined behavior will occur when you try to sort against it.

Sign up to request clarification or add additional context in comments.

5 Comments

ive already fixed that,check my question,still doesnt work :(
Then post your input file (test2.txt) as well and I'll take a deeper look.
Also, you still have a for-loop bug in your corrected question.
where is it?I use for(int i=0;i<7;i++),now i store my data in mass[0]
@kaylum if that is true I would not be able to read my array from file,but it reads it and prints everything correctly,but it still cant sort
0
    for (i = 1; i < n; i++)
          for (j = 0; j < n - i; j++) 
          {
             if (strcmp(mass[j].name, mass[j + 1].name) > 0) 
             {
                temp = mass[j];
                mass[j] = mass[j + 1];
                mass[j + 1] = temp;
             }
          }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.