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
Contributor
TheDevil
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.

Please use plain text.
Contributor
TheDevil
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

Please use plain text.
Developer
TobyKaos
Posts: 67
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
Please use plain text.
Contributor
TheDevil
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.
Please use plain text.
Developer
TobyKaos
Posts: 67
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
Please use plain text.
Contributor
TheDevil
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?
Please use plain text.
Developer
TobyKaos
Posts: 67
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
Please use plain text.
Contributor
TheDevil
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! 

Please use plain text.
Developer
TobyKaos
Posts: 67
Registered: ‎12-12-2012
My Device: playbook
My Carrier: free

Re: File Saving

Great
Please use plain text.