C 内部回路执行一次分段故障(堆芯转储)
我稍微重新构造了您的代码,但尝试尽可能接近您的版本。我用valgrind仔细检查了下面的溶液,它是无泄漏的 测试文件中的文件内容如源代码中所述,假设其结构如本例所示:C 内部回路执行一次分段故障(堆芯转储),c,C,我稍微重新构造了您的代码,但尝试尽可能接近您的版本。我用valgrind仔细检查了下面的溶液,它是无泄漏的 测试文件中的文件内容如源代码中所述,假设其结构如本例所示: #include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct { int m_id; char m_name[20]; char m_dept[20]; int m_salary;
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct
{
int m_id;
char m_name[20];
char m_dept[20];
int m_salary;
}Employee ;
int main()
{
FILE *fptr;
char ch;
int flag=0;
int j=0;
int static i ;
Employee *e=malloc(1 * sizeof(Employee));
fptr=fopen("srs.txt","r");
if(fptr==NULL)
{
printf("\nError in opening file 1!!!\n");
return 0;
}
printf("\nFile opened successfully\n");
printf("\n\n file content :\n\n");
while((fscanf(fptr,"%c",&ch))!=EOF)
{
{
while((fscanf(fptr,"%d%s%s%d",&e[i].m_id,&e[i].m_name,&e[i].m_dept,&e[i].m_salary)==4))
{
e=(Employee *)realloc(e,sizeof(Employee)+i+1);
i++;
flag=1;
}
}
}
for(j=0;j<=i;j++)
{
printf("\nEMPLOYEE ID IS : %d",e[j].m_id);
printf("\nEMPLOYEE NAME IS : %s",e[j].m_name);
printf("\nEMPLOYEE DEPT IS : %s",e[j].m_dept);
printf("\nEMPLOYEE SALARY IS : %d",e[j].m_salary);
}
if(flag==0)
printf("\nRec not found 2 \n");
fclose(fptr);
return 0;
}
/* file contain following data , number of employee not fixed
ID NAME DEPT SALARY
1 SAM CAE 111
2 DHAN CAE 222
3 PRINCE DEVP 333
*/
意思是没有前导空行或说明。可以添加它,但我想重点讨论如何动态地增长数据结构
1 SAM CAE 111
2 DHAN CAE 222
3 PRINCE DEVP 333
1 realloce,sizeofEmployee+i+1->realloce,SizeOffemployee*++i+1//remove i++在这之后2 j下一次请更加努力地提出您的问题,并描述您想要实现的目标以及您迄今为止的尝试:如果我的答案解决了您的问题,请接受:D@SRS如果你能提供反馈那就太酷了如果这就是你要找的,如果没有,请提供更多信息:D谢谢!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct
{
int m_id;
char* m_name;
char* m_dept;
int m_salary;
} Employee;
/*
* Contract
* --------
* This program tries to open a file named 'srs.txt' and assumes the following
* structure in each line:
* <integer> <string> <string> <integer>
*/
int main()
{
FILE *fptr;
// Think of 'e' as a pointer to an array of pointers to 'Employee' structs that
// will grow on demand
Employee **e= (Employee**) malloc(1 * sizeof(Employee**));
fptr=fopen("srs.txt","r");
if(fptr==NULL)
{
printf("\nError in opening file 1!!!\n");
return 0;
}
printf("\nFile opened successfully\n");
printf("\n\n file content :\n\n");
int tmp_id;
char* tmp_name = (char*)malloc(sizeof(char)*20);
char* tmp_dept = (char*)malloc(sizeof(char)*20);
int tmp_salary;
int i = 0;
while( (fscanf(fptr,"%d%s%s%d",&tmp_id,tmp_name, tmp_dept, &tmp_salary)) == 4 )
{
// Create a new employee on the heap
Employee* employee = (Employee*) malloc (1 * sizeof(Employee));
employee->m_id = tmp_id;
employee->m_name = tmp_name;
employee->m_dept = tmp_dept;
employee->m_salary = tmp_salary;
// Reserve memory for next iteration
// the outer scope of this while loop needs to free the memory
// pointed to by 'tmp_name' and 'dept_name'!!!
tmp_name = (char*)malloc(sizeof(char)*20);
tmp_dept = (char*)malloc(sizeof(char)*20);
i = i + 1;
e=(Employee **)realloc( e,(sizeof(Employee**)*i) );
e[i-1] = employee;
}
free(tmp_name);
free(tmp_dept);
int j= 0;
for(j=0;j<i;j++)
{
printf("\nEMPLOYEE ID IS : %d",e[j]->m_id);
printf("\nEMPLOYEE NAME IS : %s",e[j]->m_name);
printf("\nEMPLOYEE DEPT IS : %s",e[j]->m_dept);
printf("\nEMPLOYEE SALARY IS : %d\n",e[j]->m_salary);
}
fclose(fptr);
// Clean up the heap memory
for(j=(i-1);j>=0;j--){
free(e[j]->m_name);
free(e[j]->m_dept);
free(e[j]);
}
free(e);
return 0;
}