![]() |
Problem with fscanf
I'm writing a Qt application which is using an old C-library named GLM. It is used to load vertex data from .obj-files. When reading the files it uses fscanf() to read out the numbers before storing these in some arrays.
Running an application using the library on a Ubuntu desktop works flawlessly, but when running on a real N900 device it reads out only the integers -1, 0 and 1 - which are not even the rounded equivalents of the floating point numbers in the file! I could of course dive into reading the file using Qt instead, with hope that it would fix the problem, but I'm wondering if this is a common issue or whether it could be fixed in some other way? |
Re: Problem with fscanf
It would be a big bug in libc if that really happened without any problems in the source code. Most probably, it's just a bad code. Do you have a small example of the fscanf call and a file on which it fails?
|
Re: Problem with fscanf
Quote:
The relevant part of the code which does the reading is this: Code:
while(fscanf(file, "%s", buf) != EOF) { Code:
v 0.956776 2.407918 1.846345 http://www.cs.jhu.edu/~subodh/457/code/gltutor/glm.h http://www.cs.jhu.edu/~subodh/457/code/gltutor/glm.c In addition I'm printing the values to the console using this loop: Code:
for(int i = 0; i < numvertices; i++) { |
Re: Problem with fscanf
A quick check (scanf("%f", &blah); printf("%f", blah);) shows that it should work. Maybe you can try your app with valgrind to make sure there are no strange overflows?
Are you printing the values out after the whole loop, or right after the scanf? |
Re: Problem with fscanf
Hello,
as far as I know, scanf uses the current locale setting to determine which char is used as "decimal_point". You can view your locale settings with the command "locale". With locale settings for germany for example: Code:
Code:
~ $ locale -ck decimal_point |
Re: Problem with fscanf
@viraptor: Thanks for checking! You pointed me in the direction of the solution, which is the one nicolai mentions. I printed out the values with printf instead of qDebug() and I figured that printed it out with commas instead of dots.
@nicolai: Thanks a bunch for pointing that out :) My locale uses commas as decimal points, so that's exactly why it couldn't read the file. I'm using Code:
setlocale(LC_NUMERIC, "en_US"); Is this a good way to do this? |
All times are GMT. The time now is 01:14. |
vBulletin® Version 3.8.8