Cocos2d-x reads local files

Source: Internet
Author: User

In the company in the project development needs to read local files, so on the Internet to search for the following operations on the Cocos2d-x file reading, with two methods can be achieved, one is to use C ++, the other is the Cocos2d-x Code as follows:

// Read the file (the parameters are file names and text boxes respectively)

Void gameregistry: readfile (const char * pfilename, uilabel * plabel)

{

/* Method 1

Ifstream infile;
Infile. Open (pfilename); // open the file
String psavestr; // used to save the file content of a row.
For (string STR; Getline (infile, STR);) // read a row

{

Gbktoutf8 (STR, "GBK", "UTF-8"); // Chinese transcoding, which is also based on the Internet

Psavestr + = STR + "\ n ";

}

// Set the text box content

Plabel-> settext (psavestr );

Plabel-> setfontname (" ");

// Close the file

Infile. Close ();

*/

// Method 2

// Obtain the absolute path of the file in the system
String filepath = ccfileutils: sharedfileutils ()-> fullpathforfilename (pfilename );
// Read content
Unsigned char * Data = NULL;
// The number of bytes to read. If the read fails, the value is 0.
Unsigned long Len = 0;
Data = ccfileutils: sharedfileutils ()-> getfiledata (filepath. c_str (), "r", & Len );
// Unsigned char * to sting
STD: String my_std_string (reinterpret_cast <const char *> (data), Len );
// Chinese Transcoding
Gbktoutf8 (my_std_string, "GBK", "UTF-8 ");
Plabel-> settext (my_std_string );
Plabel-> setfontname (" ");
// Release the memory
If (LEN> 0 & Data)
{
Delete [] data;
}
}

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.