Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Highlighted
Contributor
Posts: 35
Registered: ‎11-21-2012
My Device: 9810
My Carrier: o2
Accepted Solution

Weird int problem

I am trying to work out the percentage of file loaded and instead of getting a value between 0 and 100 im getting values like -33000 - can somebody help to find the reason why please?

 

bytesRead = (int)infile.gcount();
		
		if(bytesRead > 0){
		
			completeBytesRead += bytesRead;
			per = (((double)completeBytesRead)/((double)fileSize))*100;
			percent = per;
			printf("file percent %d \n", percent);
			fflush( stdout );
			dialog_set_progress_toast_level(dialog, percent);

 thank you for any help you can provide.

Developer
Posts: 1,523
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Weird int problem

[ Edited ]

Just a guess - is completeBytesRead initially initialized to zero?

 

How do you initialize fileSize?

 

 

Btw, for better readability this line

 

per = (((double)completeBytesRead)/((double)fileSize))*100;

can be rewritten as:

per = (double)completeBytesRead / fileSize * 100;

 

'/' and '*' have the same priority, so this line will be processed left to right.

fileSize will be promoted to double automatically.

 


Andrey Fidrya, @zmeyc on twitter
Contributor
Posts: 35
Registered: ‎11-21-2012
My Device: 9810
My Carrier: o2

Re: Weird int problem

I init completeBytesRead like...

 

int completeBytesRead, bytesRead, percent = 0;

 

and I init fileSize like..

 

fileSize = filestatus.st_size;

 

is this wrong? cant load to a device for an hour or so to try any different.

 

Thanks for the tips on brackets.

 

 

Developer
Posts: 1,523
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Weird int problem

int completeBytesRead, bytesRead, percent = 0;

this is equivalent to:

int completeBytesRead; // uninitialized
int bytesRead; // uninitialized
int percent = 0;

This should fix it:
int completeBytesRead = 0, bytesRead = 0, percent = 0;

Andrey Fidrya, @zmeyc on twitter
Contributor
Posts: 35
Registered: ‎11-21-2012
My Device: 9810
My Carrier: o2

Re: Weird int problem

it certainly did fix it - thank you.

 

regards.