Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm getting segmentation fault using fseek()

Here is the code that is giving me the error of Segmentation fault..

void hire_skill()
{
    int linec = 0;
    FILE *p;
    p = fopen("/home/suraj/Coding/PBL/Details/hiree.txt", "r");
    if (p == NULL)
    {
        printf("\nFile did not open\n");
    }

    char c;
    c = fgetc(p);
    while (c != EOF)
    {
        if (c == '\n')
        {
            linec++;
        }
        c = fgetc(p);
    }
    fclose(p);
    printf("\nNumber of lines :\t%d\n", linec);

    FILE *ptrr;
    ptrr = fopen("/home/suraj/Coding/PBL/Details/hiree.txt", "r");
    if (ptrr == NULL)
    {
        printf("\nFile did not open\n");
    }
    rewind(ptrr);
    for (int i = 0; i < linec; i++)
    {
        fscanf(ptrr, "%s", hiree_login[i].name);
        fscanf(ptrr, "%d", hiree_login[i].age);
        fscanf(ptrr, "%s", hiree_login[i].gender);
        fscanf(ptrr, "%d", hiree_login[i].uid);
        fscanf(ptrr, "%s", hiree_login[i].skill);
        fscanf(ptrr, "%lld", hiree_login[i].phno);
    }
    for (int i = 0; i < linec; i++)
    {
        printf("\n%s, %d, %s, %d, %s, %lld\n", hiree_login[i].name, hiree_login[i].age, hiree_login[i].gender, hiree_login[i].uid, hiree_login[i].skill, hiree_login[i].phno);
    }
    fclose(ptrr);
}

And here is the structure i’m using to get values from the file and store it

struct hireeLogin
{
    int age;
    char name[20];
    char gender[1];
    int uid;
    char skill[20];
    long long int phno;
} hiree_login[MAX1]; //MAX1 = 50..

The whole code is on my github account : https://github.com/Suru-web/PBL/blob/main/Emp.c

I tried a few irrelevant things, but none of them worked. maybe i dont know much about this, so i would like anyone to help me fix my code. Thank you!!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In this line ,

fscanf(p, "%s", hiree_login[i].name);

File pointer p is already closed. You may need to use ptrr

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading