Search code examples
cinternationalizationdoublescanffrench

Reading double with fscanf on both english and french LANG


I have an issue with the portability of my code between french and english computers. Indeed, english uses point to separate double, as french uses comma. The problem is that I can't read a number written in english (with a point) on a french computer, because it expects a comma.

What's the good way to do it ?

I though about making a french and an english file, but I think a better solution should exist

My code for the line with the double is actually :

errcd *= fscanf(file, "%lf\n", &N->biais1->l[i]);

Solution

  • you could read the number in as a character string and then convert it to a double....

    int n;
    double N;
    char dummy[100];
    fscanf("%s",dummy);
    for (n=0;n<100;n++) 
    { /* here you need to make a conversion , to . (or . to ,) */
       if (dummy[n]==',') dummy[n]='.';
       if (dummy[n]==\0) break;
    }
    N = atof(dummy);