0

I have a large textfile that has data for my program stored. It's basically a CSV file, consisting of float numbers, one in each line. Example:

6.3580820e+03
6.3589883e+03
6.3592695e+03
6.3592695e+03
6.3635508e+03
6.3695820e+03

I know how to read a file line for line, but then I will still end up with an array with all the data in my memory. I would prefer to access a distinct line e.g. kind of readFromLine:32 Is there a way to do that without loading or reading the whole file once? If not how should I convert my data? SQLite?

DarkCell
  • 170
  • 1
  • 11

1 Answers1

0

I would prefer to access a distinct line e.g. kind of readFromLine:32 Is there a way to do that without loading or reading the whole file once?

You can use NSInputStream to read the file, and the input stream will manage reading portions of the file into memory as it needs them.

If not how should I convert my data? SQLite?

The trouble with CSV files, at least for your application, is that the only way to know where a given line can be found is to read the file from the beginning and count lines. If you had an index that told you the offset from the beginning of the file for each line, or for every 10th or 100th line, or whatever, then you could look up the position of the line (or the closest preceding line) in the index and start reading your data file from the offset indicated by the index entry. Using a database format like SQLite would be another good strategy. You could also break up the large file into smaller pieces, so that loading just the relevant part of the file isn't so resource intensive.

Caleb
  • 124,013
  • 19
  • 183
  • 272