std::fread
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <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。由读取的字符的数目的流的文件位置指示符前进.原文:
Reads up to
count
objects into the array buffer
from the given input stream stream
as if by calling std::fgetc size
times for each object, and storing the results, in the order obtained, into the successive positions of buffer
, which is reinterpreted as an array of unsigned char. The file position indicator for the stream is advanced by the number of characters read.如果对象是不
TriviallyCopyable
,该行为是未定义.原文:
If the objects are not
TriviallyCopyable
, the behavior is undefined.如果发生错误,产生的值的流的文件位置指示器
原文:
If an error occurs, the resulting value of the file position indicator for the stream is
不确定的。如果读取的部分元素,它的价值是不确定的
原文:
indeterminate. If a partial element is read, its value is indeterminate
目录 |
[编辑] 参数
buffer | - | 要读取的阵列中第一个对象的指针
原文: pointer to the first object in the array to be read |
size | - | 每个对象的大小(以字节为单位)
|
count | - | 要读取的对象的数目
原文: the number of the objects to be read |
[编辑] 返回值
成功读取的对象的数量,这可能是小于
count
如果发生错误或结束的文件条件. 原文:
Number of objects read successfully, which may be less than
count
if an error or end-of-file condition occurs. 如果
size
count
是零,fread
返回零,并执行任何其他操作.原文:
If
size
or count
is zero, fread
returns zero and performs no other action.[编辑] 示例
#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); }
输出:
1 2
[编辑] 另请参阅
stdin,文件流或缓冲区的读取格式的输入 原文: reads formatted input from stdin, a file stream or a buffer (函数) | |
得到一个字符串从一个文件流 原文: gets a character string from a file stream (函数) | |
写入到一个文件中 (函数) | |
C documentation for fread
|