-1

I have a ASCII file which I want to read byte by byte into a Byte buffer. I am clueless and confused between many aspects. Can anyone guide me with the correct way to do it?

Any help is appreciated.

Sibir
  • 313
  • 2
  • 6
  • 20
  • Why on earth would you want to read a file byte by byte? What's wrong with using [NSData dataWithContentsOfURL] (or however it's spelled, you look it up yourself). – gnasher729 Nov 04 '14 at 10:36
  • why kind of aspects do confuse you so much? because I'm not seeing any in your post... – holex Nov 04 '14 at 11:06
  • I have a file which represents a tree structure (non-readable). So I want the hexadecimal values for each component of the file in a `Byte` buffer. @holex @gnasher729 – Sibir Nov 04 '14 at 11:44

1 Answers1

0

I finaly got the answer here. So what I did is,

NSMutableString *bundlePath = [NSMutableString stringWithString:
           [[NSBundle mainBundle]pathForResource:@"excercise1" ofType:nil]];

NSData *myData = [NSData dataWithContentsOfFile:bundlePath];
uint8_t * bytePtr = (uint8_t  * )[myData bytes];
NSInteger totalData = [myData length] / sizeof(uint8_t);

NSLog(@"Data byte chunk: ");
for (int i = 0 ; i < totalData; i ++)
{
    NSLog(@" %x", bytePtr[i]);
}

and it worked as I wanted it to be. I got the bytes in an array.

Community
  • 1
  • 1
Sibir
  • 313
  • 2
  • 6
  • 20