| 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 fwrite(constvoid* buffer,std::size_t size,std::size_t count,std::FILE* stream); | ||
Writes up tocount binary objects from the given arraybuffer to the output streamstream. The objects are written as if by reinterpreting each object as an array ofunsignedchar and callingstd::fputcsize times for each object to write thoseunsignedchars intostream, in order. The file position indicator for the stream is advanced by the number of characters written.
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.
Contents |
| buffer | - | pointer to the first object in the array to be written |
| size | - | size of each object |
| count | - | the number of the objects to be written |
| stream | - | output file stream to write to |
Number of objects written successfully, which may be less thancount if an error occurred.
Ifsize orcount is zero,fwrite returns zero and performs no other action.
#include <array>#include <cstdio>#include <vector> int main(){// write buffer to fileif(std::FILE* f1=std::fopen("file.bin","wb")){std::array<int,3> v={42,-1,7};// underlying storage of std::array is an array std::fwrite(v.data(), sizeof v[0], v.size(), f1);std::fclose(f1);} // read the same data and print it to the standard outputif(std::FILE* f2=std::fopen("file.bin","rb")){std::vector<int> rbuf(10);// underlying storage of std::vector is also an arraystd::size_t sz=std::fread(rbuf.data(), sizeof rbuf[0], rbuf.size(), f2);std::fclose(f2);for(std::size_t n=0; n< sz;++n)std::printf("%d\n", rbuf[n]);}}
Output:
42-17
(C++11) | prints formatted output tostdout, a file stream or a buffer (function)[edit] |
| writes a character string to a file stream (function)[edit] | |
| reads from a file (function)[edit] | |
C documentation forfwrite | |