Advertisements
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!!
>Solution :
In this line ,
fscanf(p, "%s", hiree_login[i].name);
File pointer p
is already closed. You may need to use ptrr