gpt4 book ai didi

c - 内部循环执行一次段错误(核心转储)

转载 作者:行者123 更新时间:2023-11-30 20:42:38 24 4
gpt4 key购买 nike

#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
*/

最佳答案

我稍微重组了您的代码,但尝试尽可能接近您的版本。我用 valgrind 仔细检查了下面的解决方案,它没有泄漏。

假设测试文件中的文件内容(如源代码中所述)具有如下示例所示的结构:

1       SAM         CAE         111
2 DHAN CAE 222
3 PRINCE DEVP 333

意味着没有前导空行或描述。它可以添加,但我想重点关注如何动态增长数据结构。

#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;
}

关于c - 内部循环执行一次段错误(核心转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43594438/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com