0

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);
}
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • You should check for `fopen` (etc) errors. – Fiddling Bits Jan 10 '20 at 18:19
  • 1
    Could you at least attach to your debugger to the program and give us a traceback of where the error happened? – Charles Duffy Jan 10 '20 at 18:19
  • 1
    BTW, please make sure tags have a rational relationship to the problem. The `bash` tag would make sense if your problem was specific to bash (error didn't happen when you ran your C program from any other shell), but it makes no sense otherwise. Similarly, use the `linux` tag when the problem doesn't happen on other operating systems (or at least when your program encounters the error when using Linux-specific facilities), the `gcc` tag when it doesn't happen on other compilers (or happens in code using gcc-only pragmas), etc. – Charles Duffy Jan 10 '20 at 18:21
  • Attaching a debugger also lets you build a better [mre], since knowing where the problem happens lets you remove code that isn't needed to create that specific problem. (See also "Tricks for Trimming" at http://sscce.org/). – Charles Duffy Jan 10 '20 at 18:23
  • 1
    `void main()` isn't quite correct. The standard main function in C should return an int (either `int main (void)` or `int main(int argc, char **argv)`) – Elias Van Ootegem Jan 10 '20 at 18:24
  • 1
    Clean up the code warnings. They tell you exactly where the problem is (as described in the answer below). – jmq Jan 10 '20 at 18:42

1 Answers1

0

This line is wrong:

if(fscanf(fp,"%s %d %d %d",nome,valore1,valore2,valore3)!=EOF){

The parameters are supposed to be addresses of variables. Note that nome is already an address because arrays decay to pointers.

Fix:

if(fscanf(fp,"%s %d %d %d",nome,&valore1,&valore2,&valore3)!=EOF){
SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87