Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get byte data from file in object c
    primarykey
    data
    text
    <p>I read file from <code>fcntl(fd, F_GLOBAL_NOCACHE, 1);</code> to read file from Disk not from Cache. After reading file, i can only get string data. Now i want to after reading file, i get byte data of file to NSMutableData. How can i do that? Thanks in advance.</p> <pre><code> if (fd) { fcntl(fd, F_GLOBAL_NOCACHE, 1); NSMutableData* myData = [[NSMutableData alloc] init]; while(YES) { // store the length before addition NSUInteger previousLength = [myData length]; // increase the length so we can write more bytes [myData increaseLengthBy:300]; // read bytes from stream directly at the end of our data size_t len = fread([myData mutableBytes] + previousLength, 300, 1, fd); // set data length [myData setLength:previousLength + len]; // if end-of-file exit the loop if (len == 0) { break; } [myData appendBytes:buffer length:len]; } // use your 'myData' NSLog(@"dataFile: %@",myData); [myData release]; </code></pre> <p>Please give me suggestions? thanks</p> <p><strong>UPDATE2:</strong> Now i have another problem: i want to read file direct from disk not from Cache. I used below code but it seem not work, it still read from Cache :</p> <pre><code> NSString *url= [NSString stringWithFormat:@"%@/demo.abc"]; const char *c_sd_url = [url UTF8String]; NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; FILE * fd = fopen(c_sd_url, "rb"); if (fd) { fcntl(fd, F_GLOBAL_NOCACHE, 1); fseek(fd, 0, SEEK_END); long sz = ftell(fd); fseek(fd, 0, SEEK_SET); char *buf = malloc(sz); NSLog(@"before %s",buf); assert(buf != NULL); assert(fread(buf, sz, 1, fd) == 1); NSLog(@"after %s",buf); NSMutableData *data= [NSMutableData dataWithBytesNoCopy:buf length:sz freeWhenDone:YES]; NSLog(@"%@",data); } </code></pre> <p>I used <code>fcntl(fd, F_GLOBAL_NOCACHE, 1);</code> after fopen(). Please give me any suggestion. Thanks much</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload