0

I have this file which I need to read the first bytes to check the information. I don't need to load the whole file, only the beginning.. The code in C is, more or less, what follows. It is a big code, so I just wrote the basic functionality here.

Now I want to make it 100% Objective-C, but I cannot find a way to do it properly.

FILE *f;
char *buf;

f = fopen ("/Users/foo/Desktop/theFile.fil", "rb");

if(f) {
fseek(f , 0 , SEEK_END);
size = ftell(f);
rewind (f);
buf = (char*) malloc (sizeof(char)*size);
switch( ntohl(*(uint32 *)buf) ) {
    case 0x28BA61CE:
    case 0x28BA4E50:
        NSLog(@"Message");
    break;
}

fclose(f);
free (buf);

The most close I got to this is what follows:

NSData *test = [[NSData alloc] initWithContentsOfFile:filePath];

This gets me all the binary, but anyway, I got stuck. Better try to start all over..

Any help appreciated!

4
  • why don't use built-in way? +[NSString stringWithContentsOfFile] for example? Commented Jan 19, 2012 at 23:44
  • 1
    Your code looks wrong to begin with. You're malloc'ing a buffer the entire size of the file... and then inspecting that buffer without even reading anything. And you don't need a buffer that large if your stated goal is to just read the beginning of the file. Commented Jan 19, 2012 at 23:47
  • @KevinBallard this is only a temporary solution. My goal is to use Obj-C, so I thought I could put the effort into memory and buffer details when I come to it, rather than making the best code in C just to replace it.. Anyway, it is only to give an idea of how I want to compare the values inside the switch statement. I have already a list of hex values I wish to use. Commented Jan 20, 2012 at 9:40
  • @GiancarloMariot: I don't mean it's not "the best code", I mean you aren't reading the file. Commented Jan 20, 2012 at 20:10

4 Answers 4

4

Well, valid C code is valid Objective-C code. So this is already in Objective-C.

What's your actual goal? What are you trying to do with the file? Is there a reason you can't use NSData?

Sign up to request clarification or add additional context in comments.

2 Comments

I want to identify the data inside the files. The first 8 bytes in hex are parts of a list I already have. But, for starters, the hex becomes inverted when I try the Obj-C approach. Second, I wish not to store all file on memory. I just wish to use Objective-C to keep my project homogeneous and well documented. Could be in C, my if it is possible to be implemented, I rather prefer Obj-C.
@GiancarloMariot: No, “the hex becomes inverted” when you try the pure C code, because that code includes a function call (to ntohl) to byte-swap those first four bytes. The hex does not “become inverted” with the Cocoa one-liner, because there is nothing in it to do such a swap. As I said in my answer, you can use the exact same switch, including the byte-swap, after the Cocoa reading code.
1

C code is already Obj-C. It's perfectly reasonable to just use what you're already doing. But if you're dead-set on using Obj-C objects to perform this, you want to take a look at NSInputStream.

Comments

0

The most close I got to this is what follows:

NSData *test = [[NSData alloc] initWithContentsOfFile:filePath];

This gets me all the binary, but anyway, I got stuck.

It's not clear where you're stuck, because that's the (simplest) correct way to read a file in one big slurp in Cocoa. You've successfully read the file; there's nothing more to do for that.

If you're looking to proceed to the switch statement, the pointer to the bytes that it read is [test bytes]. That's the pointer that you will want to assign to buf. See the NSData documentation.

4 Comments

I am stuck because I can't make the same data in the switch statement using Objective-C. I don't know how to load only a part of the file into memory, I don't know what is the best way to perform it and I never find any examples of file partial byte reading anywhere.. I think the apple documentation is too subjective and lacks practical examples..
@GiancarloMariot: Well, you didn't ask about reading only part of the file into memory. The pure-C code in your question reads the whole file just as the Cocoa one-liner does. You should ask a new question about the partial-file goal.
@PeterHosey: Nitpick: his C code actually doesn't read the file at all. It just finds the size of the file, allocates a buffer of that size, and then inspects the garbage memory that he just allocated.
@KevinBallard: True; I'd forgotten that (I'd previously upvoted your comment on the question). If it worked, the C code would do the same as the Cocoa code. As they are, the Cocoa code is more correct.
0

Well.. I sorted that out.. And did what follows.

Cheers and thanks for the help!

NSString *filePath = [[NSString alloc] initWithFormat:@"/Users/foo/Desktop/theFile.fil"];

NSData *data = [NSData dataWithContentsOfFile:filePath];
NSUInteger len = [data length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [data bytes], len);

NSNumber *size = [[NSNumber alloc] initWithUnsignedLong:len/2^20];

NSLog(@"%@", size);

switch( ntohl(*(uint32 *)byteData) ) {
    case 0x28BA61CE:
    case 0x28BA4E50:
        NSLog(@"Message");
    break;
}
[size release];
[filePath release];

Comments

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.