How to read only "N" bytes from a specified file?
如何从指定的文件中只读取“N”字节?
4 个解决方案
#1
25
If you want random access to the contents of the file in a manner similar to having loaded it via NSData but without actually reading everything into memory, you can use memory mapping. Doing so means that the file on disk becomes treated as a section of virtual memory, and will be paged in and out just like regular virtual memory.
如果您希望以类似于通过NSData加载文件的方式对文件内容进行随机访问,但实际上不需要将所有内容读入内存,那么可以使用内存映射。这样做意味着磁盘上的文件将被视为虚拟内存的一部分,并将像常规的虚拟内存一样被页进页出。
NSError * error = nil;
NSData * theData = [NSData dataWithContentsOfFile: thePath
options: NSMappedRead
error: &error];
If you don't care about getting filesystem error details, you can just use:
如果您不关心获得文件系统错误细节,您可以使用:
NSData * theData = [NSData dataWithContentsOfMappedFile: thePath];
Then you would just use NSData's -getBytes:range:
method to pull out specific pieces of data, and only the relevant parts of the file will actually be read from permanent storage; they'll also be eligible to be paged out too.
然后您只需使用NSData的-getBytes:range:方法来提取特定的数据片段,并且只有文件的相关部分会从永久存储中读取;他们也有资格被调出。
#2
23
-[NSFileHandle readDataOfLength:].
- - - - - -[NSFileHandle readDataOfLength:]。
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
NSData *fileData = [handle readDataOfLength:N];
[handle closeFile];
#3
2
If you want to avoid reading the entire file, you can just use the standard C I/O functions:
如果您想避免阅读整个文件,您可以使用标准的C I/O函数:
#include <stdio.h>
...
FILE *file = fopen("the-file.dat", "rb");
if(file == NULL)
; // handle error
char theBuffer[1000]; // make sure this is big enough!!
size_t bytesRead = fread(theBuffer, 1, 1000, file);
if(bytesRead < 1000)
; // handle error
fclose(file);
#4
0
Open the file:
打开文件:
NSData *fileData = [NSData dataWithContentsOfFile:fileName];
Read the bytes you want:
阅读你想要的字节:
int bytes[1000];
[fileData getBytes:bytes length:sizeof(int) * 1000];
#1
25
If you want random access to the contents of the file in a manner similar to having loaded it via NSData but without actually reading everything into memory, you can use memory mapping. Doing so means that the file on disk becomes treated as a section of virtual memory, and will be paged in and out just like regular virtual memory.
如果您希望以类似于通过NSData加载文件的方式对文件内容进行随机访问,但实际上不需要将所有内容读入内存,那么可以使用内存映射。这样做意味着磁盘上的文件将被视为虚拟内存的一部分,并将像常规的虚拟内存一样被页进页出。
NSError * error = nil;
NSData * theData = [NSData dataWithContentsOfFile: thePath
options: NSMappedRead
error: &error];
If you don't care about getting filesystem error details, you can just use:
如果您不关心获得文件系统错误细节,您可以使用:
NSData * theData = [NSData dataWithContentsOfMappedFile: thePath];
Then you would just use NSData's -getBytes:range:
method to pull out specific pieces of data, and only the relevant parts of the file will actually be read from permanent storage; they'll also be eligible to be paged out too.
然后您只需使用NSData的-getBytes:range:方法来提取特定的数据片段,并且只有文件的相关部分会从永久存储中读取;他们也有资格被调出。
#2
23
-[NSFileHandle readDataOfLength:].
- - - - - -[NSFileHandle readDataOfLength:]。
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
NSData *fileData = [handle readDataOfLength:N];
[handle closeFile];
#3
2
If you want to avoid reading the entire file, you can just use the standard C I/O functions:
如果您想避免阅读整个文件,您可以使用标准的C I/O函数:
#include <stdio.h>
...
FILE *file = fopen("the-file.dat", "rb");
if(file == NULL)
; // handle error
char theBuffer[1000]; // make sure this is big enough!!
size_t bytesRead = fread(theBuffer, 1, 1000, file);
if(bytesRead < 1000)
; // handle error
fclose(file);
#4
0
Open the file:
打开文件:
NSData *fileData = [NSData dataWithContentsOfFile:fileName];
Read the bytes you want:
阅读你想要的字节:
int bytes[1000];
[fileData getBytes:bytes length:sizeof(int) * 1000];