I have a .txt file with floating point values on each line:
3.14 5.221 0.55 4.77
6.12 2.13 9.33 1.2
0.99 4.22 3.56 8.95 3.125
9.0575 4.897542 120.2324 55.555
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 256
int main(int argc, char *argv[])
{
if(argc !=2)
{
printf("Invalid format.\n");
return -1;
}
FILE *fin = fopen(argv[1], "rt");
if(!fin)
{
printf("ERROR opening the file.\n");
return 1;
}
char buf[MAX];
int i = 0;
float v[MAX], n;
while(fgets(buf, MAX, fin))
{
buf[strlen(buf)-1] = '\0';
char *word = strtok(buf, " ");
while(word != NULL)
{
printf("Word : %s <-> %.3f\n", word, atof(word));
//v[i] = atof(word);
//i++;
n = atof(word);
word = strtok(NULL, " ");
printf("%f\n", n);
}
}
fclose(fin);
return 0;
}
I wanna print the sets sorted by the number of digits in the decimal part. I'm thinking of using qsort maybe, but I tried placing each value in an array and only garbage values were printed. Why is that?