|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Junior Member
Iscritto dal: Mar 2014
Città: Napoli
Messaggi: 7
|
[C] Errore nella scrittura di un file binario.
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."). 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);
}
Codice:
typedef struct{
char mat[9];
short v_e;}exam;
Grazie mille. OS: Ubuntu 12.04 LTS - 64 bit; Compilatore : GNU GCC Compiler. Ultima modifica di darth89 : 02-04-2014 alle 14:31. |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Oct 2010
Messaggi: 1609
|
Sono un po' arrugginito con il C ma se non ricordo male la gets() inserisce in fondo alla stringa il new line finale.
Prova a togliere l'ultimo carattere dalla stringa letta e poi stampala per vedere cosa viene eliminato |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 00:39.


















