OC gets a string from a file or URL, and writes a file

Source: Internet
Author: User

    • OC Reads or writes files
1 /**2 * Initwithcontentsoffile: Get content from File3 * Initwithcontentsofurl: Get content from URL4 * WriteToFile: Write file5 * atomically: atomicity (the file will not be interrupted when it is written, and will not be interrupted by any other tasks or events until the execution is completed.) )6  */7 #import<Foundation/Foundation.h>8 9 intMainintargcConst Char*argv[]) {Ten @autoreleasepool { One         A         //get a string from a file or URL -Nserror *error;//error:<# (nserror *__autoreleasing *) #> level Two pointers, so just define an object and pass it in. -         //encoding:<# (nsstringencoding) #> character encoding, defined as an enumeration type, so it can be represented by a number (nsutf8stringencoding = 4) theNSString *str1=[[nsstring Alloc]initwithcontentsoffile:@"/users/apple/desktop/test.txt"Encoding4error:&ERROR]; -  -NSLog (@"%@", str1);//Prints the result if the file does not exist (NULL) or if there is no permission there will be a problem -          +         //URL Basic Syntax -         //protocol Name://domain or IP: port/file path? parameters +         //http://www.weather.com.cn/adat/sk/101010100.html(Beijing Weather) ANSString *str2=[[nsstring Alloc]initwithcontentsofurl:[nsurl urlwithstring:@"http://www.weather.com.cn/adat/sk/101010100.html"] Encoding:4error:&error];//printing results will be related to the page atNSLog (@"%@", str2); -          -         //the results of the query are as follows: -         /*{"Weatherinfo": {"City": "Beijing", "Cityid": "101010100", "temp": "9", "WD": "Southwest Wind", "WS": "Level 2", "SD": "22%", "WSE": "2", " Time ":" 10:35 "," Isradar ":" 1 "," Radar ":" JC_RADAR_AZ9010_JB "," NJD ":" No Live "," qy ":" 1015 "}} -          */ -          in         //Write File -NSString *str3=@"This phrase will be written to the file"; toBOOL ISTRUE=[STR3 WriteToFile:@"/users/chengaojian/desktop/newfile.txt"Atomically:yes Encoding:4error:&error];//automically means write all at once, and if there is an error in the middle, it will not be written +NSLog (@"Write%@", Istrue==true?@"Success":@"failed"); -     } the     return 0; *}

OC gets a string from a file or URL, and writes a file

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.