首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::fread

Defined in header <cstdio>

?

?

std::size_t fread( void* buffer, std::size_t size, std::size_t count, std::FILE* stream );

?

?

读到count对象进入数组中。buffer从给定的输入流stream好像通过打电话std::fgetcsize每个对象的时间,并按所获得的顺序将结果存储到buffer的数组被重新解释为unsigned char.流的文件位置指示符是由读取的字符数来预测的。

如果对象不是TriviallyCopyable,该行为是未定义的。

如果发生错误,流的文件位置指示符的结果值是不确定的。如果读取部分元素,则其值不确定。

参数

buffer

-

pointer to the first object in the array to be read

size

-

size of each object in bytes

count

-

the number of the objects to be read

stream

-

input file stream to read from

返回值

成功读取的对象数,可能少于count如果出现错误或文件结束情况.

如果sizecount是零,fread返回零,不执行其他操作。

二次

代码语言:javascript
复制
#include <iostream>
#include <cstdio>
#include <fstream>
#include <vector>
int main()
{
    // prepare file
    std::ofstream("test.txt") << 1 << ' ' << 2 << '\n';
    std::FILE* f = std::fopen("test.txt", "r");
 
    std::vector<char> buf(4); // char is trivally copyable
    std::fread(&buf[0], sizeof buf[0], buf.size(), f);
 
    for(char n : buf)
        std::cout << n;
 
    std::vector<std::string> buf2; // string is not trivially copyable
// this would result in undefined behavior
//    std::fread(&buf2[0], sizeof buf2[0], buf2.size(), f);
}

二次

产出:

二次

代码语言:javascript
复制
1 2

二次

另见

scanffscanfsscanf

reads formatted input from stdin, a file stream or a buffer (function)

fgets

gets a character string from a file stream (function)

fwrite

writes to a file (function)

c Fread文件

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com