I have a huge .txt file (like 100mb) and I don't want to load all the content into a NSString. So how can I read line by line this file?
I would like something like:
while ( endOfFile is not reached )
line = readline();
I have a huge .txt file (like 100mb) and I don't want to load all the content into a NSString. So how can I read line by line this file?
I would like something like:
while ( endOfFile is not reached )
line = readline();
It's also possible using C code (because ObjectiveC is a superset of C); there's the fgets
function that reads exactly one line.
FILE *file = fopen([filename UTF8String], "r");
char buffer[256];
while (fgets(buffer, sizeof(char)*256, file) != NULL){
NSString* line = [NSString stringWithUTF8String:buffer];
NSLog(@"%@",line);
}
The only tricky part is the 256
you see in the code. You need to make sure that this number is greater than the number of characters in the longest line of your text file.