| I/O manipulators | ||||
| Print functions(C++23) | ||||
| C-style I/O | ||||
| Buffers | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++20) | ||||
| Streams | ||||
| Abstractions | ||||
| File I/O | ||||
| String I/O | ||||
| Array I/O | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
| Synchronized Output | ||||
(C++20) | ||||
| Types | ||||
| Error category interface | ||||
(C++11) | ||||
(C++11) |
| Types and objects | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <cstdio> | ||
std::size_t fread(void* buffer,std::size_t size,std::size_t count,std::FILE* stream); | ||
Reads up tocount objects into the arraybuffer from the given input streamstream as if by callingstd::fgetcsize times for each object, and storing the results, in the order obtained, into the successive positions ofbuffer, which is reinterpreted as an array ofunsignedchar. The file position indicator for the stream is advanced by the number of characters read.
If the objects are notTriviallyCopyable, the behavior is undefined.
If an error occurs, the resulting value of the file position indicator for the stream isindeterminate. If a partial element is read, its value is indeterminate.
Contents |
| 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 |
Number of objects read successfully, which may be less thancount if an error or end-of-file condition occurs.
Ifsize orcount is zero,fread returns zero and performs no other action.
fread does not distinguish between end-of-file and error, and callers must usestd::feof andstd::ferror to determine which occurred.
#include <cstddef>#include <cstdio>#include <fstream>#include <iomanip>#include <iostream>#include <vector> int main(){// Prepare filestd::ofstream("test.txt")<<1<<' '<<2<<'\n';std::FILE* f=std::fopen("test.txt","r"); std::vector<char> buf(4);// char is trivially copyableconststd::size_t n= std::fread(&buf[0], sizeof buf[0], buf.size(), f); std::cout<<"Read "<< n<<" object"<<(n>1?"s":"")<<": "<<std::hex<<std::uppercase<<std::setfill('0');for(char n: buf)std::cout<<"0x"<<std::setw(2)<<static_cast<short>(n)<<' ';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);}
Possible output:
Read 4 objects: 0x31 0x20 0x32 0x0A
| reads formatted input fromstdin, a file stream or a buffer (function)[edit] | |
| gets a character string from a file stream (function)[edit] | |
| writes to a file (function)[edit] | |
C documentation forfread | |