1

I want to read one text file and perform some action on that data.

file size is 67 mb

how can i read.

file is in text format.

its working in simulateor but giving memory warning in device and crashes.

code is

NSString *content = [[[NSString alloc] initWithContentsOfFile:fileName usedEncoding:nil error:nil] autorelease];

crashes when this sentence complete.

Thanks,

Shyam parmar

Erik S
  • 1,939
  • 1
  • 18
  • 44
GameLoading
  • 6,688
  • 2
  • 33
  • 57
  • 1
    Could you maybe give us some additional information on what you are doing with the text then? So we know how to approach the problem. If you'd need all the data that's in there, you would probably still end up with 67MB, but in another format. If, however, it's something like a XML or JSON file, it's much easier to process. – Erik S Mar 21 '11 at 08:28

3 Answers3

3

You haven't given code, but if you are using stringWithContentsOfFile to get an entire file consider using NSInputStream or stdio to read it and process or display it more incrementally.

Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
2

Fasttracks, try what Peter suggested. The problem is loading it all at once, since you have about 20 MB available for your own app, i believe. If you'd use a NSInputStream, you can load it in pieces, due to which you wont fill up the entire memory at once. Also read this answer to another question: Objective-C: Reading a file line by line

Community
  • 1
  • 1
Erik S
  • 1,939
  • 1
  • 18
  • 44
  • i have a 70 mb file , using this code to read file doesn't hep me it increases memory linearly. can any one help me? – GameLoading Mar 21 '11 at 06:21
1

Do you start reading the file from - (void) viewDidLoad? That might be the problem. Try start reading it in a different thread, like: [self performSelectorInBackground:@selector(method) withObject:nil];

Mikael
  • 3,572
  • 1
  • 30
  • 43
  • And you don't initialize that class from viewDidLoad? The reason that I'm asking is that I had a similar problem, and I had a crash due to a timeout when the Default was loading. What kind of text are you trying to read? Isn't possible to split up the text file into several files? – Mikael Mar 18 '11 at 13:31
  • The crash is likely because it tries to load 67 megabytes in the memory at once, which gives a huge memory error. You will still get this error if you perform it in the background ;) – Erik S Mar 21 '11 at 13:59
  • @Erik, yes, that's why I suggested splitting up the text file into several files :) – Mikael Mar 22 '11 at 14:22
  • Yea, i guessed :) I mainly meant to react on your main answer ;) – Erik S Mar 22 '11 at 15:08