i have a segmentation error when i try to lunch the Cprogramm on this code and i don't knok why. The goal of the code is to create mini-files.dat which contatins the name of the person in nomi.txt + a flag that indicates if the -(neagative)values are > soglia(i set soglia in the main). nomi.txt is a file like this:
Mike 12 -16 90
Carl 23 -40 -42
Jonh 18 5 40
Bob -90 12 16
Code as follows:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char stringa[30];
void creaFile(stringa nome,int flag){
FILE *fp=fopen(nome,"a+");
fprintf(fp,"[%s][%d]\n",nome,flag);
fclose(fp);
}
void creaBinarioNegativi(int soglia){
FILE* fp = fopen("nomi.txt","r+");
stringa nome;
int flag=0;
int cont=0;
int valore1=0;
int valore2=0;
int valore3=0;
while(!feof(fp)){
if(fscanf(fp,"%s %d %d %d",nome,valore1,valore2,valore3)!=EOF){
if(valore1<0) {
cont++;
}
else if(valore2<0){
cont++;
}
else if(valore3<0){
cont++;
}
if(cont>=soglia){flag=1;}
creaFile(nome,flag);
}
}
fclose(fp);
}
void main(){
int soglia = 2;
creaBinarioNegativi(soglia);
}