I have a large text resource in my bundle. It’s a CSV containing lines like
0,1,100,2.2345
It’s over 9MB. What’s the best way to open and sequentially read it in so I can do something like:
myObject->initData(col0, col1, col2, col3);
(Which just stuffs the float value into one of a number of multidimensional arrays indexed by the integers in the file.)
I tried reading it into a string using [NSString stringWithContentsOfFile:] and using an NSScanner to loop over it, but I don’t want to double up my memory usage, even temporarily. It was also quite slow.
What’s the best way to do this?
Thanks
You can use
NSFileHandleor C APIs to read incrementally, or you can use mmap withNSData.The remainder is basic cstring buffer handling, or you could use
NSStringline-by-line.Your life may be easier if you can export it as a sequence of binary floats, rather than CSV.