Int and float from file to array, been stumped for 5 hours

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Crux
Posts: 10
Joined: Sun May 25, 2008 1:46 pm

Int and float from file to array, been stumped for 5 hours

Post by Crux »

while (fgets(tempTime,11,songPort)!=NULL) {

if (fgets(tempButton,4,songPort)!=NULL) {

noteTime[arrayCounter]=atof(tempTime);
noteButton[arrayCounter]=atoi(tempButton);

}

printf("noteTime: %.1f\n", noteTime[arrayCounter]);
printf("noteButton: %i\n", noteButton[arrayCounter]);

arrayCounter++;

}
It reads from a file that looks like this:
.1
2
.3
4
.5
6
0.7
8
0.9
10
and the output is this:
0.000
2
0.000
4
0.000
6
0.000
8
.9
10
Notice the final float, it shows up? WTH?

Can anybody explain to me why nearly half the values return 0 (zero)? Thanks I've been messing with it for at least 5 hours with no luck.[/quote]
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

There doesn't appear to be anything wrong in the code you showed, so it's probably memory corruption or some similar problem (arrays too small, etc). Test your code out on a PC; if you have a specific (complete) piece of code that works fine on a PC but doesn't work on the PSP then maybe someone here can point out a problem.
BenHur
Posts: 28
Joined: Sat Oct 20, 2007 5:26 pm

Post by BenHur »

@Crux
Shouldn't your output contain the words "noteTime:" and "noteButton:"? Did it really print 3 digits after the decimal point (0.000), even though you specified 1 digit?

Anyway, if atof conversion fails it returns 0.0. Thus I would check the input strings and make sure there are no funny/unexpected chars.
Post Reply