GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Sep 30, 2014 3:53 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: [SOLVED] gtk_init() and fscanf
PostPosted: Tue Nov 29, 2011 7:07 pm 
Offline
Familiar Face

Joined: Thu May 26, 2011 11:11 am
Posts: 11
Hi,
I'm trying to develop a gtk+ interface which needs to read float values from a file using fscanf.

I found out that if I'm going to read a file before gtk_init() everything goes well (code below):

Code:
int main (int argc, char *argv[])
{
    FILE *fi;
    strcpy(name, argv[1]);     
   
    if ((fi=fopen(name,"r"))==NULL)
    {
         printf("Error opening input file\n");
         return EXIT_FAILURE;
    }   


    while (fscanf(fi,"%g",&y)!=EOF)
    {
        printf("%g\n",y);
        g_usleep(1000000);
    }
   
    fclose(fi);

    gtk_init(NULL,NULL);
    ...


In this first case the output reports

Code:
0.003
0.002
0.001


which indeed is the content of the file.


while if I put the read-code just before gtk_init() (which is actually what I need to do) as in the following code:

Code:
int main (int argc, char *argv[])
{
    FILE *fi;

    strcpy(name, argv[1]);     
   
    if ((fi=fopen(name,"r"))==NULL)
    {
         printf("Error opening input file\n");
         return EXIT_FAILURE;
    }   

    gtk_init(NULL,NULL);

    while (fscanf(fi,"%g",&y)!=EOF)
    {
        printf("%g\n",y);
        g_usleep(1000000);
    }
   
    fclose(fi);

    ...


the result is an infinite print of:
Code:
0
0
0
...



I was wondering if it's somehow related to the arguments of gtk_init(&argc,&argv);:

Thanks in advance


Last edited by Nyquist on Tue Nov 29, 2011 10:04 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: gtk_init() and fscanf
PostPosted: Tue Nov 29, 2011 8:51 pm 
Offline
Familiar Face

Joined: Tue Nov 29, 2011 7:59 pm
Posts: 11
Show results of printf("%p", fi) before and afte gtk_init(,)


Top
 Profile  
 
 Post subject: Re: gtk_init() and fscanf
PostPosted: Tue Nov 29, 2011 8:55 pm 
Offline
Familiar Face

Joined: Thu May 26, 2011 11:11 am
Posts: 11
Before:
Code:
0x98a9700



After:
Code:
0x990e2f0


seems correct..


Top
 Profile  
 
 Post subject: Re: gtk_init() and fscanf
PostPosted: Tue Nov 29, 2011 10:03 pm 
Offline
Familiar Face

Joined: Thu May 26, 2011 11:11 am
Posts: 11
Ok, I found out a solution.

It seems that the rendering of floating values in C functions for formatted input such as sscanf and fscanf, is affected by the local language set on the computer. In English countries, floating points are represented with a point while in other ones they may be written with commas. If I get it right, gtk_init() forces the rendering of floating values with local rules so that the input files with float values and points can't be parsed correctly.

Instead of using specific functions to change local parameters after gtk_init() I simply decided to change the default operative system language to Fully English. Now it works.

Sources:
http://git.xonotic.org/?p=xonotic/netra ... 45d492bd00


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group