Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取iphone中文件内容的大小_Iphone_Objective C_File_File Io - Fatal编程技术网

获取iphone中文件内容的大小

获取iphone中文件内容的大小,iphone,objective-c,file,file-io,Iphone,Objective C,File,File Io,我正在为iphone编写一个应用程序,使用NSData读取文本文件 NSFileHandle *fileRead; // this may lead to crash or memory issue, if the file size is about 10 mb or more NSData *data = [fileRead readDataOfLength:(entire contents of the file)]; 例如,如果文件大小为8KB,我们可以以每循环1kb的速度在

我正在为iphone编写一个应用程序,使用NSData读取文本文件

NSFileHandle *fileRead;

// this may lead to crash or memory issue, if the file size is about 10 mb or more

NSData *data =   [fileRead readDataOfLength:(entire contents of the file)]; 
例如,如果文件大小为8KB,我们可以以每循环1kb的速度在8次迭代中读取它

在读取文件之前,我们如何找到文件内容的大小,以便以优化的方式编写代码以有效地读取文件


请给我你的建议….

iPhone有所有标准的POSIX API,所以你可以使用stat(“file.txt”,“&st”),其中st是一个“struct stat”。“st.st_size”成员将提供以字节为单位的文件大小。

iPhone具有所有标准POSIX API,因此您可以使用stat(“file.txt”,&st),其中st是一个“struct stat”。“st.st_size”成员将以字节为单位提供文件大小。

如果您确实想读取整个文件,只需使用
readdatatoendofile
即可,无需事先确定长度


从NSFileHandle中,您可以通过以下方式找到长度:首先
参见ktoEndOffile
,然后
OffsetFile
。如果您有实际的文件名,则可以使用NSFileManager的
属性FiteMatPath:error:
来检索长度(与C的
stat
一样)。或者,就这一点而言,您实际上可以对NSFileHandle的
fileDescriptor
方法返回的文件描述符使用
stat
,或者
fstat

如果您确实想读取整个文件,只需使用
readdatatoendofile
,而不用担心事先确定长度

从NSFileHandle中,您可以通过以下方式找到长度:首先
参见ktoEndOffile
,然后
OffsetFile
。如果您有实际的文件名,则可以使用NSFileManager的
属性FiteMatPath:error:
来检索长度(与C的
stat
一样)。或者,就这一点而言,您实际上可以对NSFileHandle的
fileDescriptor
方法返回的文件描述符使用
stat
,或
fstat