For the More Curious: Reading and Writing to the Filesystem

In addition to archiving and NSData’s binary read and write methods, there are a few more methods for transferring data to and from the filesystem. One of them, Core Data, is coming up in Chapter 23. A couple of others are worth mentioning here.

You have access to the standard file I/O functions from the C library. These functions look like this:

F​I​L​E​ ​*​i​n​F​i​l​e​ ​=​ ​f​o​p​e​n​(​"​t​e​x​t​f​i​l​e​"​,​ ​"​r​t​"​)​;​ c​h​a​r​ ​*​b​u​f​f​e​r​ ​=​ ​m​a​l​l​o​c​(​s​o​m​e​S​i​z​e​)​;​ f​r​e​a​d​(​b​u​f​f​e​r​,​ ​b​y​t​e​C​o​u​n​t​,​ ​1​,​ ​i​n​F​i​l​e​)​;​ F​I​L​E​ ​*​o​u​t​F​i​l​e​ ​=​ ​f​o​p​e​n​(​"​b​i​n​a​r​y​f​i​l​e​"​,​ ​"​w​"​)​;​ f​w​r​i​t​e​(​b​u​f​f​e​r​,​ ​b​y​t​e​C​o​u​n​t​,​ ...

Get iOS Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.