fread与fread_s读取文件(二进制文件)
fread()是c库函数,利于移植,使用缓存,效率较read()高。
原型:
size_t fread(void *buffer, size_t size, size_t count, FILE * stream);
要注意的是它的返回值,如果读取到了文件尾,返回值小于count,可以使用feof()函数检测出来,返回真。
PS:返回值代表的是某种类型的size的个数。
下面程序按照1024k(一次大小为sizeof(char))一次读取二进制文件。
include <stdio.h>
#include <string.h> #define BUFFSIZE 1024 int main(int argc, char **argv){ char buff[BUFFSIZE];
FILE *fd = fopen (argv[], "rb");
int count, errno=; bzero (buff, BUFFSIZE);
while (!feof (fd)){
count = fread (buff, sizeof (char), BUFFSIZE, fd);
int n = feof (fd);
printf ("%d,%d\n", count, n);
printf ("%s\n",strerror (errno));
}
return ;
}
fread_s读取流的数据。 fread 此版本的具有安全增强功能,如 CRT中的安全功能所述。
size_t fread_s(
void *buffer,
size_t bufferSize,
size_t elementSize,
size_t count,
FILE *stream
);
-
数据的存储位置。
- bufferSize
-
目标缓冲区的大小(以字节为单位)。
- elementSize
-
写入字节的项的大小。
- count
-
要读取的项目最大数。
- stream
-
为 FILE 结构的指针。
有关错误代码的更多信息,请参见 _doserrno、errno、_sys_errlist和_sys_nerr。
此功能锁定其他线程。 如果需要非固定版本,请使用 _fread_nolock。
|
功能 |
必需的标头 |
|---|---|
|
fread_s |
<stdio.h> |
// crt_fread_s.c
// Command line: cl /EHsc /nologo /W4 crt_fread_s.c
//
// This program opens a file that's named FREAD.OUT and
// writes characters to the file. It then tries to open
// FREAD.OUT and read in characters by using fread_s. If the attempt succeeds,
// the program displays the number of actual items read. #include <stdio.h> #define BUFFERSIZE 30
#define DATASIZE 22
#define ELEMENTCOUNT 2
#define ELEMENTSIZE (DATASIZE/ELEMENTCOUNT)
#define FILENAME "FREAD.OUT" int main( void )
{
FILE *stream;
char list[];
int i, numread, numwritten; for ( i = ; i < DATASIZE; i++ )
list[i] = (char)('z' - i);
list[DATASIZE] = '\0'; // terminal null so we can print it // Open file in text mode:
if( fopen_s( &stream, FILENAME, "w+t" ) == )
{
// Write DATASIZE characters to stream
printf( "Contents of buffer before write/read:\n\t%s\n\n", list );
numwritten = fwrite( list, sizeof( char ), DATASIZE, stream );
printf( "Wrote %d items\n\n", numwritten );
fclose( stream );
} else {
printf( "Problem opening the file\n" );
return -;
} if( fopen_s( &stream, FILENAME, "r+t" ) == ) {
// Attempt to read in characters in 2 blocks of 11
numread = fread_s( list, BUFFERSIZE, ELEMENTSIZE, ELEMENTCOUNT, stream );
printf( "Number of %d-byte elements read = %d\n\n", ELEMENTSIZE, numread );
printf( "Contents of buffer after write/read:\n\t%s\n", list );
fclose( stream );
} else {
printf( "File could not be opened\n" );
return -;
}
}
fread与fread_s读取文件(二进制文件)的更多相关文章
- PHP读取文件函数fread,fgets,fgetc,file_get_contents和file函数的使用总结
fread().fgets().fgetc().file_get_contents() 与 file() 函数用于从文件中读取内容. 1.fread() fread()函数用于读取文件(可安全用于二进 ...
- php学习笔记--高级教程--读取文件、创建文件、写入文件
打开文件:fopen:fopen(filename,mode);//fopen("test.txt","r"): 打开模式:r 仅仅读方式打开,将文件指针指向 ...
- php中读取文件内容的几种方法
1.fread string fread ( int $handle , int $length ) fread() 从 handle 指向的文件中读取最多 length 个字节.该函数在读取完最多 ...
- Matlab如何循环读取文件
循环读取图片第一种方法①List =dir('*.jpg'); %如需其它图片格式支持,可以自己[重载dir()]函数,实现查找所有图片文件的功能,%如果图片是其它路径,可以用 ["路径&q ...
- PHP读取文件的多种方法
1.传统的方法 fopen, fclose feof:file.end of file 例子: $file_handle = fopen("c:\\myfile.txt", &qu ...
- PHP读取文件的常见方法
整理了一下PHP中读取文件的几个方法,方便以后查阅. 1.fread string fread ( int $handle , int $length ) fread() 从 handle 指向的文件 ...
- PHP中读取文件的几个方法
整理了一下PHP中读取文件的几个方法,方便以后查阅. 1.fread string fread ( int $handle , int $length ) fread() 从 handle 指向的文件 ...
- php中读取文件内容的几种方法。(file_get_contents:将文件内容读入一个字符串)
php中读取文件内容的几种方法.(file_get_contents:将文件内容读入一个字符串) 一.总结 php中读取文件内容的几种方法(file_get_contents:将文件内容读入一个字符串 ...
- python用二进制读取文件
python二进制读取文件 很多时候,都需要用到二进制读取文件.毕竟很多信息不是以文本的形式存储.例如:图片,音乐等等.这些东西都是有规则的二进制文件. 在python中,二进制读取采用rb的方式. ...
随机推荐
- 优化SQLServer
由于SQLServer,数据文件mdf过大,造成系统异常卡 一. 更改隔离级别 ALTER DATABASE [B2EC] SET SINGLE_USER WITH ROLLBACK IMMEDIAT ...
- 集合框架以及Map(一)
集合又称容器,编程思想中对其的定义为持有对象 我们在使用集合或者数组时得到最多的异常就是数组下表越界异常 Java.lang.ArrayIndexOutOfBoundsException这篇文章我们不 ...
- 判定 java 对象死亡的过程
- Spring_Spring与AOP_AspectJ基于XML的实现
一.前置通知 import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.as ...
- Fill Table Row(it’s an IQ test question)
Here is a table include the 2 rows. And the cells in the first row have been filled with 0~4. Now yo ...
- JavaScript高级编程———数据存储(cookie、WebStorage)
JavaScript高级编程———数据存储(cookie.WebStorage) <script> /*Cookie 读写删 CookieUtil.get()方法根据cookie的名称获取 ...
- 很赞的一个教程: React.js 小书
很赞, React.js 小书 http://huziketang.com/books/react/ 推荐阅读入门, 照着来一遍,能会个七七八八, 更多的还需要多写 import Re ...
- LeetCode赛题395----Longest Substring with At Least K Repeating Characters
395. Longest Substring with At least K Repeating Characters Find the length of the longest substring ...
- ODP.Net Tips
Overview Oracle Data Provider for .NET是Oracle 提供的.Net版本的数据库连接组件.下载路径. 使用的核心DLL是Oracle.DataAccess.dll ...
- ArcGIS 地类净面积计算工具
地类净面积计算工具可以自己定义图层.字段.地类代码计算任意图层的椭球面积.线状地物扣除.零星扣除和其他扣除,计算地类净面积计算:可以用于二调数据图斑地类.规划地块和基本农田等等需要计算净面积的都可以. ...