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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Highlighted
Contributor
Posts: 19
Registered: ‎01-03-2013
My Device: BB 10 Devloper
My Carrier: Vodafone
Accepted Solution

File Saving

Hi,

Using the example at http://developer.blackberry.com/native/documentation/playbook/com.qnx.doc.native_sdk.devguide/com.qn...

Im trying to save data in a file for my game. My code is as follows:

std::string path1 = CCFileUtils::sharedFileUtils()->getWriteablePath();
	path1.append("save.dat");
	FILE *fp = fopen(path1.c_str(), "wb");
	CCLog("path is %s",path1.c_str());

	     if (!fp) {
	    	 CCLog("error!!!!!");
	     }
	     else
	     {
	    	 int page;
	    	 int firstRead = fscanf(fp, "%i", &page);
	    	 CCLog("file read value is %d %d",firstRead,page);
	    	 if(firstRead == -1)
	    	 {
	    		 fprintf(fp,"%i",globalVolume);
	    		 fprintf(fp,"%i",globalLevel);
	    		 fprintf(fp,"%i",_timeAttack);
	    		 fprintf(fp,"%i",_maxStreak);
	    		 for(int i = 0;i < 30; i++)
	    		 {
	    			 fprintf(fp,"%i",((CCString *)_score->objectAtIndex(i))->intValue());
	    		 }
	     }
	     }

//	     fprintf(fp, "%i", page);

	     fclose(fp);

	     fp = fopen(path1.c_str(), "wb");

	     if (!fp) {
	    	 CCLog("error!!!!!");
	     }
	     else
	     {
	    	 int page;
	    	 	    	 int firstRead = fscanf(fp, "%i", &page);
	    	 	    	 CCLog("file read value is %d %d",firstRead,page);
	     }
	     fclose(fp);

 

The ouput that I get is :

 

path is /accounts/1000/appdata/org.cocos2dx.Test.testDev__Test2c5a53de/data/save.dat

cocos2d-x debug info file read value is -1 -1323428880

read value is -1 -1323428880

read value is -1 -1323428880.

 

 

I am testing this on the simulator. Is there anything wrong with what Im doing? Does file saving work on the simulator?

 

Thanks.

Contributor
Posts: 19
Registered: ‎01-03-2013
My Device: BB 10 Devloper
My Carrier: Vodafone

Re: File Saving

Just checked on the Device. Does not work there too. The output i get is :

 

cocos2d-x debug info path is /accounts/1000/appdata/org.cocos2dx.Test.testDev__Test2c5a53de/data/save.dat

cocos2d-x debug info file read value is -1 136197336

cocos2d-x debug info file read value is -1 136197336

Developer
Posts: 78
Registered: ‎12-12-2012
My Device: playbook
My Carrier: free

Re: File Saving

I use fwrite instead of fprintf and all is ok for me
Contributor
Posts: 19
Registered: ‎01-03-2013
My Device: BB 10 Devloper
My Carrier: Vodafone

Re: File Saving

Everything else is the same in ur code?will check this as soon as I have access to a system.
Developer
Posts: 78
Registered: ‎12-12-2012
My Device: playbook
My Carrier: free

Re: File Saving

you do not modify page value when firstRead return -1 at the first time
Contributor
Posts: 19
Registered: ‎01-03-2013
My Device: BB 10 Devloper
My Carrier: Vodafone

Re: File Saving

How do I know if it's the first time that the file is being written?
Developer
Posts: 78
Registered: ‎12-12-2012
My Device: playbook
My Carrier: free

Re: File Saving

Try to open it in read mode "rb". if file descriptor is null then open it in wb
Contributor
Posts: 19
Registered: ‎01-03-2013
My Device: BB 10 Devloper
My Carrier: Vodafone

Re: File Saving

Thanks! Using fwrite and fread did the trick for me! 

Developer
Posts: 78
Registered: ‎12-12-2012
My Device: playbook
My Carrier: free

Re: File Saving

Great