Ciao a tutti.
Durante l'esecuzione di questa function si presente un seg fault al momento della scrittura dei dati in un file binario. Il motivo dell'errore non lo riesco proprio a capire, dato che non avviene neanche la stampa del messaggio di errore ("Errore nella scrittura del file."). La creazione del file, invece, avviene correttamente.
Vi posto il codice:
codice:
#include "header.h"
void updat(FILE *fp,struct lista **head,short n)
{
char a[15];
short x,z,i,k=0;
struct lista *ptr;
FILE *fc;
exam esame[n];
puts("Inserire nome esame:");
gets(a);
while(getchar()!='\n');
strcat(a,".dat");
if(fc=fopen(a,"wb+")==NULL){puts("Errore nell'apertura del file");exit(1);}
puts("Inserire crediti esame:");
scanf("%hd",&z);
while(getchar()!='\n');
do{
ptr=*head;
puts("Quale matricola ha superato l'esame?");
for(i=1;i<=n;i++)
{
printf("%hd. %s;\n",i,ptr->info.mat);
ptr=ptr->p_next;
}
puts("Inserire l'indice seguito da return (0 per terminare).");
scanf("%hd",&x);
while(getchar()!='\n');
if(x!=0)
{
ptr=*head;
for(i=1;i<x;i++)ptr=ptr->p_next;
ptr->info.n_s++;
printf("\nVoto per %s:\n",ptr->info.c_n);
scanf("%hd",&ptr->info.v_e[ptr->info.n_s-1]);
ptr->info.cdt[ptr->info.n_s-1]=z;
ptr->info.m_e=mp(ptr->info.v_e,ptr->info.n_s,ptr->info.cdt);
strcpy(esame[k].mat,ptr->info.mat);
esame[k].v_e=ptr->info.v_e[ptr->info.n_s-1];
k++;
}
}while(x!=0);
if(fwrite(esame,sizeof(exam),k,fc)!=k){puts("Errore nella scrittura del file,");exit(1);}
fclose(fc);
}
Il tipo di dato "exam" è dichiarato nell'header:
codice:
typedef struct{
char mat[9];
short v_e;}exam;
Sapreste dirmi dove sbaglio?
Grazie mille.
OS: Ubuntu 12.04 LTS - 64 bit;
Compilatore : GNU GCC Compiler.