0

So i have a CSV file with 61 numbers or so, and depending on the first digit, if its odd or even, it goes into one set or another, but i dont how to read just the first digit of one number and then pass to the next one, while saving that number into one set or another. I have added part of the code.

void lectura(){
    float conjunto1[1000],conjunto2[1000];
    char temp[1000];
    int cont=0,i;
    char *numero;
    tGeolocalizacion *geo;
    const char coma[2] = ";";
    
    char archivo[]= "fichnum.csv";
    FILE *pFich; 
    pFich = fopen (archivo, "r");
    if (pFich==NULL)
    {
        printf ("NO se puede abrir el programa, ERROR\n");
            exit(1);
    }
    
    
    cont = 0;
    while(!feof(pFich)){
        fgets(temp,1000,pFich);
        cont++;
    }
    
    
    rewind(pFich);
    
    geo = (tGeolocalizacion*) malloc(cont*sizeof(tGeolocalizacion));
    
    for (i=0; i < cont; i++){
        
        
        //LEER COMO CHAR Y GUARDAR, DESPUES SE PASA A FLOAT 
        fgets(temp,1000,pFich);
        
        
        numero = strtok(temp,coma);
        sscanf(numero,"%f", &geo[i].latitud);
        numero = strtok(NULL,coma);
        sscanf(temp,"%f",&geo[i].longitud);
        
//This is the part im doing wrong obviously
        if (geo[0].latitud%2==0){
        geo[i].latitud=conjunto1[i];

        
    }
    printf ("f",conjunto1[i]);
    if (geo==NULL){
            printf ("NO se puede abrir el programa, ERROR\n");
            exit(1);
    }
    
    printf("el archivo se ha leido correctamente");


    
}
  • 1
    You are using `feof` incorrectly: https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong – William Pursell Nov 19 '22 at 12:42
  • You should *never* read a file to determine how many lines it contains and then rewind and re-read the file to process it. That is a (horrible) anti-pattern. – William Pursell Nov 19 '22 at 12:42
  • Ok, so what should i do? – Nachonacho Nachonacho Nov 19 '22 at 12:46
  • You should read until the data is exhausted. (eg `while( fgets(...) != NULL ){ ... }` – William Pursell Nov 19 '22 at 12:48
  • In the description, you say that you decide which set the number goes into based on the first digit, but the code uses `%2` to test, which suggests you are interested in the least significant digit (which doesn't make much sense for a floating point number, and indeed you cannot use `%` on a float), so I'm a bit confused. Are you looking at the parity of the first number in the file to determine into which set all of the number in the file should go, or do you decide for each individual number where it should go based on the parity of its integer part? – William Pursell Nov 19 '22 at 12:52
  • So i decide for each individual number based on the first digit, if its even or odd, and according to that i introduce them in a set or the other one. – Nachonacho Nachonacho Nov 19 '22 at 17:39

0 Answers0