views:

187

answers:

2

I am building a game similar to mastermind for the iPhone and I am needed to compare two strings character by character. I need to choose a string from the database and compare it with the string entered by the player.

I have tried the following but it does not seem to work. Can you please help me.

//create file handle
NSFileHandle *file;
file = [NSFileHandle fileHandleForReadingAtPath:@"4db.txt"];

//read data into file in NSData format
NSData *filedata;
filedata = [file readDataToEndOfFile];

//convert NSData to NSString
NSString *string;
string = [[NSString alloc] initWithData:filedata encoding:NSASCIIStringEncoding]; 

//convert from string to array
NSArray *lines = [string componentsSeparatedByString:@"\n"]; 

//take one of the string and store it in sword
NSString *sword = [lines objectAtIndex:0];

//convert string to char
const char *word = [sword UTF8String];
sec[0] = word[0];
sec[1] = word[1];
sec[2] = word[2];
sec[3] = word[3];

I am now assuming sec contains the characters of the string that I read from the file. It does not seem to work. Kindly help. Any other efficient method would b great if you can suggest.

A: 

You're jumping through quite a few hoops there. You can initialise a string with the contents of a file with just one statement:

NSString *str = [NSString stringWithContentsOfFile:@"db4.txt"
                                          encoding:NSASCIIStringEncoding
                                             error:NULL];

Although, in this case, you may want to ensure that the contents of the file were read correctly by providing an NSError *:

NSError *error;
NSString *str = [NSString stringWithContentsOfFile:@"db4.txt"
                                          encoding:NSASCIIStringEncoding
                                             error:&error];

if (fileContents == nil)
    NSLog (@"Error! %@", error);

Try to use Cocoa whenever you can. If you're comparing strings, it is better to convert any C-style strings to NSStrings and compare NSStrings than to drop back to C-style strings.

dreamlax
+2  A: 

So..... what's not working?

What you have is correct...ish. I'd probably do it like this:

NSString * contents = [NSString stringWithContentsOfFile:@"db4.txt" encoding:NSASCIIStringEncoding error:nil];
NSArray * lines = [contents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
NSString * firstLine = [lines firstObject];

NSString * userEnteredString = ...;
if ([firstLine isEqualToString:userEnteredString]) {
  //the first line matches what the user entered
}

EDIT

If it can't find your db4.txt file, then it's because it's looking for it in the wrong place. Try something like:

NSString * dbFile = [[NSBundle mainBundle] pathForResource:@"db4" ofType:@"txt"];
NSString * contents = [NSString stringWithContentsOfFile:dbFile encoding:NSASCIIStringEncoding error:nil];
Dave DeLong
WOW!! Finally it works. Thanks Dave :) :) NSString * dbFile = [[NSBundle mainBundle] pathForResource:@"db4" ofType:@"txt"]; did the trick :)
abhinav