The entire code below you can directly use, specifically to see a good look at the problem, I also look at others.
Who can be born skillfully.
NSString class, wrote a few simple operations called system functions.
%@ print NSString, Nsarray object.
%i Integer
%f floating Point
Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager]; Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSLog (@ "path=%@", paths); NSString *documentsdirectory = [Paths objectatindex:0]; NSLog (@ "documentsdirectory=%@", documentsdirectory); [FileManager changecurrentdirectorypath:[documentsdirectory Stringbyexpandingtildeinpath]; [FileManager createfileatpath:@ "FileName" Contents:nil Attributes:nil]; [FileManager removeitematpath:@ "Creatednewfile" error:nil]; NSString *path = [documentsdirectory stringbyappendingpathcomponent:@ "FileName"]; NSLog (@ "path=%@", Path); NSString *temp = @ "Welcome to Blog.csdn.net/qianguozheng"; int data0 = 800; float data1 = 12.34f; Nsmutabledata *writer = [[Nsmutabledata alloc] init]; [writer Appenddata:[temp datausingencoding:nsutf8stringencoding]]; [Writer Appendbytes:&data0 length:sizeof (DATA0)]; [Writer Appendbytes:&data1 LenGth:sizeof (data1)]; [Writer Writetofile:path Atomically:yes]; [Writer release]; int gData0; float gData1; NSString *gdata2; NSData *reader = [NSData Datawithcontentsoffile:path]; GDATA2 = [[NSString alloc] Initwithdata:[reader subdatawithrange:nsmakerange (0, [temp length])] Encoding: Nsutf8stringencoding]; [Reader getbytes:&gdata0 range:nsmakerange ([temp length], sizeof (GDATA0))]; [Reader getbytes:&gdata1 range:nsmakerange ([temp length] + sizeof (GDATA0), sizeof (DATA1))]; NSLog (@ "gdata0:%i gdata1:%f gdata2:%@", gData0, GData1, GDATA2);
The output is as follows:
2015-01-08 22:45:01.423 oc[21613:303] path= (
"/users/weeds/documents"
)
2015-01-08 22:45:01.424 oc[21613:303] Documentsdirectory=/users/weeds/documents
2015-01-08 22:45:01.429 oc[21613:303] Path=/users/weeds/documents/filename
2015-01-08 22:45:01.430 oc[21613:303] gdata0:800 gdata1:12.340001 gdata2:welcome to Blog.csdn.net/qianguozheng
IOS file Operations