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;
}
}