Cocos2D-x file read I/O

Source: Internet
Author: User

The cocos2d-x provides the getfiledata interface as follows:

Char * file = (char *) ccfileutils: sharedfileutils ()-> getfiledata ("testjson. JSON", "r", & size );

Other methods are as follows:

With reference to other people's programs, the path is modified:

//string path = CCFileUtils::sharedFileUtils()->getWriteablePath()+pFileName ;string path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(pFileName.c_str());

Use fullpathfromrelativepath to check the name. It seems that you want to enter a relative path. In fact, you only need to enter a file name in the resource folder, and it will recognize the path of the file. So what you want to pass in is not a path, but a parameter. The official API documentation is misleading.

Const char * fullpathfromrelativepath ( Const char * Pszrelativepath )  

Generate the absolute path of the file.

Parameters
Pszrelativepath The relative path of the file.
Returns
The absolute path of the file.
Warning
We only add the resourcepath before the relative path of the file.
Deprecated:
Please use fullpathforfilename instead.

//// Tdinvfileutils. h // mycoa2dtest /// created by Wei Zhu Quan on 13-2-27. /// # ifndef _ mycoa2dtest _ tdinvfileutils __# DEFINE _ mycocoa2dtest _ tdinvfileutils __# include <iostream> # include "cocos2d. H "using namespace cocos2d; using namespace STD;/** operation file storage and Reading */class ccreadfile {public:/** read local files, return data */static string getfilebyname (string pfilename);/** store the content to the file */static bool SaveFile (char * Pcontent, string pfilename );}; # endif/* defined (_ mycoa2dtest _ tdinvfileutils __)*/

//// Tdinvfileutils. CPP // mycoa2dtest /// created by Wei Zhu Quan on 13-2-27. //// # include "ccreadfile. H "string ccreadfile: getfilebyname (string pfilename) {// string Path = ccfileutils: sharedfileutils ()-> getwriteablepath () + pfilename; string Path = ccfileutils: sharedfileutils () -> fullpathfromrelativepath (pfilename. c_str (); cclog ("Path = % s", path. c_str (); file * file = fopen (path. c_str (), "R"); If (File) {C Har * Buf; int Len;/* Get length */fseek (file, 0, seek_end); // move to the end Len = ftell (File ); // extract the length rewind (File); // return to the in-situ cclog ("count the file content Len = % d", Len); // allocate Buf space Buf = (char *) malloc (sizeof (char) * Len + 1); If (! Buf) {cclog ("malloc space is not enough. "); return NULL;} // read the file // read into the Buf, unit size, length, file pointer int rlen = fread (BUF, sizeof (char), Len, file); Buf [rlen] = '\ 0'; cclog ("has read length = % d", rlen); cclog ("has read content = % s ", buf); string result = Buf; fclose (File); free (BUF); return result;} elsecclog ("Open File error. "); return NULL;} bool ccreadfile: SaveFile (char * Pcontent, string pfilename) {// obtain the stored file path string Path = ccfileutils: sharedfileutils () -> getwriteablepath () + pfilename; cclog ("wanna save File Path = % s", path. c_str (); // create a file pointer // path, mode file * file = fopen (path. c_str (), "W"); If (File) {fputs (Pcontent, file); fclose (File);} elsecclog ("save file error. "); Return false ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.