Cocos2d-x3.0 File Processing

Source: Internet
Author: User


1. Read content from the file

Auto sharedFileUtils = FileUtils: getInstance (); std: string ret; sharedFileUtils-> purgeCachedEntries (); std: vector
 
  
SearchPaths = sharedFileUtils-> getSearchPaths (); searchPaths. insert (searchPaths. begin (), "Misc"); sharedFileUtils-> setSearchPaths (searchPaths); std: vector
  
   
ResolutionsOrder = sharedFileUtils-> getSearchResolutionsOrder (); resolutionsOrder. insert (resolutionsOrder. begin (), "resources-ipadhd"); resolutionsOrder. insert (resolutionsOrder. begin () + 1, "resources-ipad"); resolutionsOrder. insert (resolutionsOrder. begin () + 2, "resources-widehd"); resolutionsOrder. insert (resolutionsOrder. begin () + 3, "resources-wide"); resolutionsOrder. insert (resolutionsOrder. begin () + 4, "resources-hd"); resolutionsOrder. insert (resolutionsOrder. begin () + 5, "resources-iphone"); sharedFileUtils-> setSearchResolutionsOrder (resolutionsOrder); for (int I = 1; I <7; I ++) {auto filename = StringUtils: format ("test2.16d.txt", I); // obtain the file path ret = sharedFileUtils-> fullPathForFilename (filename. c_str (); // obtain the string std: string st = FileUtils: getInstance ()-> getStringFromFile (filename ); log ("% s-> % s", filename. c_str (), st. c_str ());}
  
 

Write and read locally

Auto sharedFileUtils = FileUtils: getInstance (); std: string ret; sharedFileUtils-> purgeCachedEntries (); std: vector
 
  
SearchPaths = sharedFileUtils-> getSearchPaths (); std: string writablePath = sharedFileUtils-> getWritablePath (); std: string fileName = writablePath + "external1.txt "; char szBuf [100] = "Hello Cocos2d-xrewrwe! ";/* Write to local */FILE * fp = fopen (fileName. c_str (), "wb"); if (fp) {size_t ret = fwrite (szBuf, 1, strlen (szBuf), fp); CCASSERT (ret! = 0, "fwrite function returned zero value"); fclose (fp); if (ret! = 0) log ("Writing file to writable path succeed. ");} /*********************************/searchPaths. insert (searchPaths. begin (), writablePath); sharedFileUtils-> setSearchPaths (searchPaths); // Get external.txt from writable path std: string fullPath = sharedFileUtils-> fullPathForFilename ("external1.txt "); log ("external file path = % s", fullPath. c_str ();/* read the file */if (fullPath. length ()> 0) {fp = fopen (fullPath. c_str (), "rb"); if (fp) {char szReadBuf [100] = {0}; size_t read = fread (szReadBuf, 1, strlen (szBuf ), fp); if (read> 0) log ("The content of file from writable path: % s", szReadBuf); fclose (fp );}}
 


Determine whether a file exists

Auto s = Director: getInstance ()-> getWinSize (); auto sharedFileUtils = FileUtils: getInstance (); Label * pTTF; bool isExist = false; // determine whether the file exists isExist = sharedFileUtils-> isFileExist ("grossini.png"); pTTF = Label: createWithSystemFont (isExist? "Images/grossini.png exists": "Images/grossini.png doesn't exist", "", 20); pTTF-> setPosition (Point (s. width/2, s. height/2); this-> addChild (pTTF );



auto s = Director::getInstance()->getWinSize();        auto sharedFileUtils = FileUtils::getInstance();                        ValueMap dict;        dict["grossini.bmp"] = Value("grossini.png");        dict["grossini.xcf"] = Value("grossini.png");                sharedFileUtils->setFilenameLookupDictionary(dict);        auto sprite = Sprite::create("grossini.bmp");        this->addChild(sprite);                sprite->setPosition(Point(s.width / 2, s.height / 2));



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.