3

I have a texfile as follows:

line1
line2
line3
line4
line5
 ....

I want to read from file into two arrays of string so that line1, line3, line 5,... go into array1 and line 2, line 4, line 6,... go into array2. Each element of arrays stores one line.

Jake1164
  • 12,291
  • 6
  • 47
  • 64
DavidNg
  • 2,826
  • 6
  • 32
  • 45
  • You can't, fundamentally, read a variable-length record file by individual record. You've got to read character-by-character, or read blocks and then "deblock" them. I'd suggest the latter -- read the entire file and do "componentsSeparatedBy..." to get your individual lines. – Hot Licks Jun 27 '12 at 01:57
  • possible duplicate of [Objective-C: Reading a file line by line](http://stackoverflow.com/questions/1044334/objective-c-reading-a-file-line-by-line) – benzado Jun 27 '12 at 01:59
  • possible duplicate: https://stackoverflow.com/questions/1044334/objective-c-reading-a-file-line-by-line check out that link because it has a lot of useful information for what you're doing. – Kpmurphy91 Jun 27 '12 at 01:56

1 Answers1

5

Step 1) Read file ([NSString stringWithContentsOfFile:encoding:error:] )

Step 2) Split string ([NSString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] )

Step 3) Iterate over array and insert into your 2 arrays

borrrden
  • 33,256
  • 8
  • 74
  • 109
  • 1
    @silvansky Sure, but I don't think that is a typical case. You'd have to have a metric buttload of text before it started to cause problems. – borrrden Oct 31 '13 at 07:37