<分区>
所以我在 C 中有这个功能,可以将数据从二进制文件读取到动态数组。当我运行它时它崩溃了,我试图放置一些 printf 以了解它被卡住的位置,看起来就像是在我尝试重新分配时。 我只是找不到任何错误。希望有人能帮助我。
tipoEmprestimo *lerFichBin_Emprestimos(tipoEmprestimo *vetorEmprestimos,int *quantEmprestimos)
{
int quantlidos;
FILE *ficheiro;
ficheiro=fopen("emprestimos.dat","rb");
if (ficheiro == NULL)
{
printf("\nNao foi possivel ler o ficheiro!");
free(vetorEmprestimos);
vetorEmprestimos=NULL;
*quantEmprestimos=0;
}
else
{
quantlidos=fread(&quantEmprestimos,sizeof(int),1,ficheiro);
if (quantlidos != 1)
{
printf("\nErro ao ler ficheiro!");
}
vetorEmprestimos=realloc(vetorEmprestimos,(*quantEmprestimos)*sizeof(tipoEmprestimo));
if (vetorEmprestimos == NULL)
{
printf("\nErro ao reservar memoria!");
}
else
{
quantlidos=fread(vetorEmprestimos,sizeof(tipoEmprestimo),*quantEmprestimos,ficheiro);
if(quantlidos != *quantEmprestimos)
{
printf("\nErro ao ler ficheiro!");
}
}
}
fclose(ficheiro);
return vetorEmprestimos;
}