GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::fread(3) C++ Standard Libary std::fread(3)

std::fread - std::fread


Defined in header <cstdio>
std::size_t fread( void* buffer, std::size_t size, std::size_t count, std::FILE*
stream );


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.


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 - 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 than count if an error or
end-of-file condition occurs.


If size or count is zero, fread returns zero and performs no other action.

// Run this code


#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


scanf reads formatted input from stdin, a file stream or a buffer
fscanf (function)
sscanf
fgets gets a character string from a file stream
(function)
fwrite writes to a file
(function)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.