0

As I was trying to write code which is supposed to sort some structures in a file by a specific field (key), I noticed that my function won't read the key correctly. I don't have any idea what I am doing wrong. The code is not complete.

The constr function is supposed to read one structure at a time from the binary file, then only save the varsta array. However, if I try to see what value I obtained, the values are not the ones I gave.

This is my code:

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


typedef struct
{
    char nume[20];
    char prenume[20];
    float varsta;
} PERS;


typedef struct
{
    float key;
    int nr;
}INDEX;

int constr(FILE *f, INDEX tabl[])
{
    int n;
    n = ftell(f) / sizeof(PERS);

    int i, depl = 0;
    PERS p;
    for (i = 0; i < n; i++)
    {
        fseek(f, depl, 0);

        fread(&p, sizeof(p), 1, f);

        tabl[i].key = p.varsta;
        tabl[i].nr = i;
        depl += sizeof(PERS);

    }

    return n;
}





int main()
{
    FILE *f;
    PERS pers[3];


    if ((f = fopen("fis.txt", "wb+")) == NULL)
    {
        printf("Not ok");
        exit(1);
    }

    int i;
    for (i = 0; i < 3; i++)
    {
        scanf("%s%s%f", &pers[i].nume, &pers[i].prenume, &pers[i].varsta);
        fwrite(&pers[i], sizeof(PERS), 1, f);

    }


    INDEX tabl[3];

    int n = constr(f, tabl);

    printf("%d", tabl[2].key); //only to check if the key is correct

    fclose(f);
}
12
  • 1
    Have you stepped through the code or looked at the resulting file to be sure you're writing what you think you are? Commented Jan 21, 2019 at 17:06
  • 1
    At this line : tabl[i].key = p.varsta;: varsta is a float, but key is an int. Is this intended? Commented Jan 21, 2019 at 17:09
  • 2
    the fseek isn't needed - the position in the file will increase the right amount every time you call fread Commented Jan 21, 2019 at 17:17
  • 2
    @stark the fread and fwrite are directly applied to a PERS, it is binary. The scanf is from stdin Commented Jan 21, 2019 at 17:27
  • 1
    @MaryPoppins you write and read well the file, you just printf wrongly the key because of the format %d rather than %f or equivalent. So all is good ... except the test to check if it is good :-) Commented Jan 21, 2019 at 17:33

1 Answer 1

3

The key field is a float, but you are trying to print an integer.

Change the penultimate line in your code to

printf("%.2f\n", tabl[2].key);
Sign up to request clarification or add additional context in comments.

1 Comment

@MaryPoppins: It works for me after making the afore-mentioned change. If you have changed your code please ensure your answer contains the exact code you are currently using. Also: try examine the file using hexedit or similar program.

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.