Cocos2d-x operation: File Read causes crash

Source: Internet
Author: User


Question 1: fopen
In vs, fopen is used to process files and run through. However, a major push problem occurs when it is transplanted to Android, first, you need to understand that the development resources in vs are stored in the same directory of the execution file, and the resources are stored in the Assets Directory when transplanted to Android, you have tried the following methods before:
Char * filename = "bg.png ";
String filepath = [/font] [/color] fileutils: getinstance () [font = Arial]-> fullpathforfilename (filename );
File * fp = fopen (filepath. c_str (), "R ");

The result is still an error. When you enter the game, it is probably related to the underlying layer. The solution is to use ccfileutil of cocos2dx. We recommend that you learn it.Cocos2d-x from scratchVideo tutorial.
Char * filename = "bg.png ";
// Obtain the absolute path of the file in the system-assets/bg.png
String filepath = fileutils: getinstance ()-> fullpathforfilename (filename );
// The number of bytes to read. If the read fails, the value is 0.
Unsigned long Len = 0;
// Read content
Unsigned char * Data = fileutils: getinstance ()-> getfiledata (filepath. c_str (), "rb", & Len );

/*
Perform operations on files here
*/

// Remember to release the memory
If (LEN> 0 & Data) Delete [] data;

************** ***************
Static unsigned char * getfiledata (const char * pszfilename,
Const char * pszmode,
Unsigned long * psize
)
Pszmode:
R: open the file in read-only mode. The file must exist.
R + can open a file in read/write mode. The file must exist.
RB + read/write opens a binary file and allows reading data.
RT + read/write: open a text file and allow reading and writing.
W. Open and write only the file. If the file exists, the file length is 0, indicating that the file content will disappear. If the file does not exist, the file is created.
W + open the readable and writable file. If the file exists, the file length is cleared to zero, that is, the file content disappears. If the file does not exist, the file is created.
A. Open and write-only files as an attachment. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained. (EOF reserved)
A + opens readable and writable files by appending them. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained. (The original EOF is not retained)
WB only opens or creates a new binary file; only data can be written.
WB + enables read/write operations or creates a binary file, allowing read and write operations.
Wt + read/write opens or creates a text file; read/write is allowed.
At + read/write opens a text file, allowing you to read or append data at the end of the text.
Open a binary file through AB + read/write, and allow you to read or append data to the end of the file.
*/


Question 2: tinyxml
When tinyxml is used to read XML files, my previous practices are as follows:
// Create an animation based on XML data
String xmlfile_path = "BG. xml ";
// Read the XML file of the action
Tinyxml2: xmldocument * Doc = new tinyxml2: xmldocument ();
Doc-> LoadFile (xmlfile_path.c_str ());
Tinyxml2: xmlelement * ani_node = doc-> rootelement ();
Tinyxml2: xmlelement * action_node = ani_node-> firstchildelement ("action ");

First read the XML file to a buff, and then let tinyxml parse the buff, so that tinyxml bypasses the bug of File Reading failure.
// Create an animation based on XML data
String xmlfile_path = "BG. xml ";
// Obtain the absolute path of the file in the system
String xmlfilepath = [/font] fileutils: getinstance () [font = Arial]-> fullpathforfilename (xmlfile_path.c_str ());
// The number of bytes to read. If the read fails, the value is 0.
Unsigned long Len = 0;
// Read content
Unsigned char * Data = [/font] fileutils: getinstance () [color = # 362e2b] [font = Arial]-> getfiledata (xmlfilepath. c_str (), "r", & Len );
// Read the XML file of the action
Tinyxml2: xmldocument * Doc = new tinyxml2: xmldocument ();
Doc-> parse (char *) data, Len );
Tinyxml2: xmlelement * ani_node = doc-> rootelement ();
Tinyxml2: xmlelement * action_node = ani_node-> firstchildelement ("action ");

For more information, you can log on to the mentor Yiyou network to learn video tutorials.

"Ice bucket challenges"The activity is waiting for everyone to actively challenge themselves and become a designer. There are only a few dozen pieces ~

Cocos2d-x operation: File Read causes crash

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.