gpt4 book ai didi

C - 将函数加载到数据库 - 调用堆栈错误/段错误

转载 作者:太空宇宙 更新时间:2023-11-04 07:30:31 26 4
gpt4 key购买 nike

<分区>

我用 C 编写数据库已有一段时间了,昨天我遇到了一个我无法处理的错误。案例是,当我在写loading from .txt函数时,程序从.txt读取数据后出现了一些错误。 (例如,它是“84683-3478-”而不是“1993-5-13”)我试图改变某事,然后发生了废话,从那时起我一直收到这样的错误:

“段错误”-> 在 Linux 上“调用堆栈内存”-> 在 Windows 上

我真的不知道问题出在哪里,因为我没有做太多改变(放置文本的方式),但错误发生了。我正在为你粘贴功能。你能帮忙吗?

void load(struct player *main)
{
int bad;
struct player *act, *prev; // act- actual prev - previous
FILE *plik;
char a;
char text[l]; // l=15;
if((plik = fopen("savings.txt", "r")) == NULL)
{
printf("No savings\n");
return;
}
if(fgets(text, l, plik) == NULL)
{
printf("No saved things\n");
return;
}
printf("These are your savings\n");
do
{
printf("%s", text);
}while(fgets(text, l, plik) != NULL);
fclose(plik);
printf("\nType the name of the file you want to load (with .txt) \n");
do
{
gets(text); // here program stops, no matter if the name is right or not
char *text = (char*) malloc(30);
while (getchar()!='\n')
continue;
strcat(text, ".txt");
printf("%s", *text);
if((plik = fopen(text, "r")) == NULL)
{
printf("There is no such file, type once again \n");
bad=1;
}
else bad=0;
}while(bad);
act = main->next;
while(act != NULL) //cleaning the actual data base
{
prev = act;
act = act->next;
free(prev);
}
nr_of_players = 0;
act = main;
while(fscanf(plik, "%s", act->name)!=0)
{
fscanf(plik, "%s", biez->surname);
fscanf(plik, "%d", &biez->date_y);
fscanf(plik, "%d", &biez->date_m);
fscanf(plik, "%d", &biez->date_d);
fscanf(plik, "%s", biez->position);
fscanf(plik, "%c", &a);
fscanf(plik, "%d", &biez->nr_cart);
fscanf(plik, "%d", &biez->salary);
fscanf(plik, "%d", &biez->matches);
act->id = ++nr_of_players;
act->next = (struct player*) malloc(sizeof(struct player));
prev = act;
act = act->next;
}
free(act);
prev->next = NULL;
fclose(plik);
}

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