1

I have a file that contains in the very first byte of data a number. In this case that number is 32. I have used a hex editor to confirm that (in hex) the value is "20" which equals 32 in decimal.

Can someone point me in the right direction of how to read it out of the file. I have tried about 6 different ways all of which have failed.

3
  • 3
    Can you summarize what you tried and how they failed? Commented Jun 25, 2013 at 23:55
  • 1
    Agreed. No sense in duplicating the effort when it might be a simple bug. Commented Jun 26, 2013 at 0:01
  • No, not really. It would take me pages and pages to summarize everything I've done. I have put way too much work into what should be an EASY problem. Commented Jun 26, 2013 at 1:34

1 Answer 1

2

Lots of different ways. Here's one:

NSData *data = [NSData dataWithContentsOfFile:filename];
if ([data length] > 0)
{
    const uint8_t *bytes = (const uint8_t *)[data bytes];
    uint8_t byte = bytes[0];
    NSLog(@"%d", byte);
}

or another:

NSInputStream *stream = [NSInputStream inputStreamWithFileAtPath:filename];
[stream open];
NSInteger bufferLen = 1;
uint8_t buffer[bufferLen];
NSInteger count = [stream read:buffer maxLength:bufferLen];
[stream close];
if (count > 0)
{
    NSLog(@"%d", buffer[0]);
}
Sign up to request clarification or add additional context in comments.

5 Comments

I tried both of these and the top one causes a compiler error and the bottom one returns 1 instead of 32...
@Hoodai Both of these were cut and paste from a working test project, both work fine for me, and I got "32" from both (for a file whose first byte was 0x20). On the compile error, I can't help you if you don't to tell me what the error was and on what line you got the error. On the second example, I can only say that the file you're opening via your app doesn't sound like it contains what you think it does. It seems implausible that count was greater than zero, but that this didn't return the first byte. If you upload your file somewhere, I'm happy to take a look at it.
@Hoodai Hmm, I added that to my project and it worked great (getting "32" from both of the two code snippets). If it's not working for you, I'd try cleaning the project (or, better, remove the app and reinstall) and rebuilding it. Sometimes Xcode gets confused if you edit/change a file outside of the Xcode IDE. The above patterns are not uncommon, and this is such a trivial programming question, that your problem has to be something weird like a confused project that needs to be rebuilt from scratch. Good luck.
I don't know what I was doing wrong initially, but I tried it again and both worked. However, the top one server my purposes better. Thank you very much for your assistance!
@Hoodai I'm sure it was just Xcode getting confused. I've had that problem myself occasionally when dealing with external files and a "Clean" (or worst, delete app and reinstall) often fixes it. Glad you're in business again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.