-2

I'm running a program that opens a .csv file that contains 3 different (float) values on each line. I want to separate the different values in different variables. For that I have already used strtod() funcion but it only works on Visual Studio and not on DAVE IDE (based on Eclipse). I have also tested the following code but with no success at all (it recognizes a lot of garbage and nothing similar to want I want).

f_read(&fil, &buff2[0], 25, &br);
f_close(&fil);

for (int y=0; y<25; y++){
    if ((buff2[y] == '\n') || (buff2[y] == '\r')){
        break;
    }
    else count++;
}

for (int y=0; y<count; y++){
  cut[y] = buff2[y];
}
for (int y=count; y<25; y++){
  cut[y] = NULL;
}


//token = strtok(cut, ";");

/* walk through other tokens */
/*while( token != NULL )
{
    array[j] = atof(token);
    token = strtok(NULL, ";");
    j++;
}*/


 for (int j = 0, ptr = cut; j < 3; ++j, ++ptr)
 {
        //array[j] = (float)strtof(ptr, &ptr);
        array[j] = (double)strtod(ptr, &ptr);
  }

The commented parts of the code are other tests I've made but none of those work. And the reason why I've used f_read() is because I only can use this library: http://elm-chan.org/fsw/ff/00index_e.html

An example of "cut" string is bellow:

cut[0]  char    52 '4'  
cut[1]  char    46 '.'  
cut[2]  char    50 '2'  
cut[3]  char    49 '1'  
cut[4]  char    54 '6'  
cut[5]  char    59 ';'  
cut[6]  char    48 '0'  
cut[7]  char    59 ';'  
cut[8]  char    49 '1'  
cut[9]  char    48 '0'  
cut[10] char    48 '0'  
cut[11] char    0 '\0'  
cut[12] char    0 '\0'  
cut[13] char    0 '\0'  
cut[14] char    0 '\0'  
cut[15] char    0 '\0'  
cut[16] char    0 '\0'  
cut[17] char    0 '\0'  
cut[18] char    0 '\0'  
cut[19] char    0 '\0'  
cut[20] char    0 '\0'  
cut[21] char    0 '\0'  
cut[22] char    0 '\0'  
cut[23] char    0 '\0'  
cut[24] char    0 '\0'  

After the 11th element there's nothing, but that's not the point since this is only an example and some of the lines on the .csv file have more digits (but they have the same 3 variables).

Thanks in advance.

EDIT: Problem solved! It seems that DAVE IDE doesn't like to include an headder with includes. I had to write #include in the beggining of main.c.

canibalimao
  • 27
  • 1
  • 1
  • 7
  • 2
    huh? Please show a complete, valid example. – OldProgrammer Feb 29 '16 at 17:57
  • 1
    This doesn't looks like a .csv file... Post the content of the file, it seems that you read it badly or use the read part badly. Your C-string is ended at position 14 (NUL char). Post full functional code that can be tested. – Jean-Baptiste Yunès Feb 29 '16 at 17:59
  • Post declaration of `cut[]` and how it was populated. – chux - Reinstate Monica Feb 29 '16 at 18:16
  • 1
    If all lines have 3 doubles, why not use fscanf? – stark Feb 29 '16 at 18:17
  • 1
    The standard `strtof();` require 2 arguments,not 1 like in `strtof(token);` – chux - Reinstate Monica Feb 29 '16 at 18:17
  • 'strtok' should stop at the 15th character (cut[14] == \0). Also, the first line stops at 11., am I right? Your input is shitty, you should do something with it, like a cleanup before parsing it as a CSV. – Koshinae Feb 29 '16 at 18:19
  • My csv file have 1181 lines. The "cut[]" string is just the first line of it. @stark this will be used in a microcontroller that does not have that fscanf function. I can only use this: http://elm-chan.org/fsw/ff/00index_e.html – canibalimao Mar 01 '16 at 08:40
  • @chux you're right. It was a test. Here is the try I've made with strtof (and also strtod): for (int j = 0, ptr = cut; j < 3; ++j, ++ptr) { array[j] = (float)strtof(ptr, &ptr); //array[j] = (double)strtod(ptr, &ptr); } – canibalimao Mar 01 '16 at 08:40
  • [" values: 446676608, ... 4.216"](http://stackoverflow.com/users/2141154/canibalimao) --> have hex patterns like 1A9FBE80 0x1.0dd2f1a9fbe77p+2. This indicates that a mis-alignment. I would say you are not including the proper `` files or attempting to print using the wrong specifiers. – chux - Reinstate Monica Mar 01 '16 at 14:24

1 Answers1

2

The standard C function for converting string to double is atof.
Are you using that?
Why not?

abelenky
  • 63,815
  • 23
  • 109
  • 159