iphone - How to read a random line from a textfile? -
i'm absolute beginner xcode , objective-c, hey sounded book i'm reading rory lewis ! i'm trying write simple question game ios.
this first app, except few versions "hello world" ;-) have read large number of forums, , haven't yet found i'm looking for.
so read out random line plain textfile, let's "file.txt" wich contains 500 lines. when press button, app fetch random line file.txt , present on screen. have tried number things, , feel i'm fumbling in dark ! hoping point me in right direction (a basic 1 please ;-).
something should work:
#include <stdlib.h> #include <time.h> nsstring *file = [[nsbundle mainbundle] pathforresource:@"file" oftype:@"txt"]; nsstring *filecontents = [nsstring stringwithcontentsoffile:file]; nsarray *lines = [filecontents componentsseparatedbystring:@"\n"]; /* initialize random seed: */ srand ( time(null) ); /* generate random number: */ int index = rand() % [lines count]; nsstring *string = [lines objectatindex:index];
Comments
Post a Comment