0

I want to read some float value one by one from a custom file I defined "player.geo".

player.geo is a file I created using Xcode 4 ("Empty File" from the File > New menu)

I'm currently trying to do it like this:

- (id) initWithGeometryFile:(NSString *) nameOfFile
{
    NSFileHandle *geoFile = NULL;

    NSString *geoFilePath = [[NSBundle mainBundle] pathForResource:@"player" ofType:@"geo"];

    geoFile = [NSFileHandle fileHandleForReadingAtPath:geoFilePath];

    if(geoFile == NULL)
    {
        NSLog(@"Failed to open file.");
    }
    else
    {
        NSLog(@"Opening %@ successful", nameOfFile);

        NSMutableData *fileData = [[NSMutableData alloc] initWithData:[geoFile readDataOfLength:4]];

        float firstValue;
        [fileData getBytes:&firstValue length:sizeof(float)];

        NSLog(@"First value in file %@ is %f", nameOfFile, firstValue);
    }

    return self;
}

I'm not getting the expected value of -64.0, rather I'm getting 0.0.

Is this the right way to go about it?

Do I really have to read the file as a string and then parse float the string contents to get the float value?

mskfisher
  • 3,291
  • 4
  • 35
  • 48
Zhang
  • 11,549
  • 7
  • 57
  • 87
  • Please also show the code you're using to write that value to the file. – Ben Zotto Aug 06 '11 at 14:32
  • I'm not using code to write the data to file. I just type the numbers into the file inside Xcode manually. I went right click on my "geometry" folder and go New File, clicked on "Other" option under Mac OS X template section and chose "Empty". Then I start typing in some number and saved the file. – Zhang Aug 06 '11 at 14:33
  • 1
    Ah yes, then that's your problem. This won't read "textual" data that looks like numbers; it's interpreting the bytes themselves as floating point data. You should read it as text and then process that text (NSScanner, etc) – Ben Zotto Aug 06 '11 at 14:38
  • =/ Bah! I was afraid that it was treating it as text. Thanks for the answer and pointing me to the right way (NSScanner) – Zhang Aug 06 '11 at 14:47

1 Answers1

1

NSData objects deal with raw bytes, not strings. If you are typing in a string into a txt file, this will not work. If you are using NSData objects, then you will need to first write the data using the data object methods such as writeToFile:atomically:.

Alternately, you can use the NSString functions stringWithContentsOfFile and componentsSeperatedByString to generate an NSArray containing each string on it's own line, like so:

NSString *tmp;
NSArray *lines;
lines = [[NSString stringWithContentsOfFile:@"testFileReadLines.txt"] 
                   componentsSeparatedByString:@"\n"];

NSEnumerator *nse = [lines objectEnumerator];
while(tmp = [nse nextObject]) {
    NSLog(@"%@", tmp);
}
memmons
  • 40,222
  • 21
  • 149
  • 183
  • That's one of the method I found earlier, reading string by separator. I prefer using NSScanner as suggested by quixoto, seems more easier when I can scanFloat :D rather than specify symbols but this way does have its use for those peculiar times. Thanks for this extra answer anyhow. – Zhang Aug 06 '11 at 16:30