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的方式. ...
随机推荐
- java核心技术-NIO
1.reactor(反应器)模式 使用单线程模拟多线程,提高资源利用率和程序的效率,增加系统吞吐量.下面例子比较形象的说明了什么是反应器模式: 一个老板经营一个饭店, 传统模式 - 来一个客人安排一个 ...
- CodeForces 606A(水)
这道题之前没注意到at least,审题不仔细啊,两个问题解法还是有些许区别的 有at least的 #include <iostream> #include <string> ...
- Linux-debian系统 /etc/network/interface 文件解读
原文 http://wiki.slimdevices.com/index.php/SqueezeOS_networking 话说Debian系的网卡配置跟Redhat系很不一样,Redhat是放在/e ...
- C#学习笔记(基础知识回顾)之值类型和引用类型
一:C#把数据类型分为值类型和引用类型 1.1:从概念上来看,其区别是值类型直接存储值,而引用类型存储对值的引用. 1.2:这两种类型在内存的不同地方,值类型存储在堆栈中,而引用类型存储在托管对上.存 ...
- js历史记录
1. history 是什么? window上的一个对象,由来存储浏览器访问过的历史 2. 用途: 可以动态跳转任意一个已在历史记录中的地址 3..history方法: 1.forward() : 向 ...
- BZOJ2187:fraction
Sol 分情况讨论 \(\lfloor\frac{a}{b}\rfloor+1\le \lceil\frac{c}{d}\rceil-1\) 直接取 \(q=1,p=\lfloor\frac{a}{b ...
- ThreeJs 选中物体事件
选中物体变红色demo: https://threejs.org/examples/#webgl_raycast_sprite <!DOCTYPE html> <html lang= ...
- laravel之路由汇总
- Telnet初试(本地测试)
win7下开启Telnet功能: 控制面板-程序和功能- 开启服务 然后回车 这样即可完成一次请求
- Python爬虫教程-08-post介绍(百度翻译)(下)
Python爬虫教程-08-post介绍(下) 为了更多的设置请求信息,单纯的通过urlopen已经不太能满足需求,此时需要使用request.Request类 构造Request 实例 req = ...