Header file: <cstdio/stdio. h>
Purpose:Reads data from a file stream.A maximum of Count elements can be read, with each element size byte. If the call is successful, the number of actually read elements is returned. If the call is unsuccessful or the file is read, 0 is returned.
Function prototype:Size_t fread (void * buffer, size_t size, size_t count, file * Stream)
Resolution:
Buffer: memory address used to receive data
Size: the number of bytes to read and write, in bytes.
Count: Number of read/write requestsSizeByte data items,Each element isSizeBytes.
Stream: input stream
Returned value: Number of actually read Elements.If the returned value isCountDifferent,The end of the file or an error may occur..
SlaveFerrorAndFeofGet error information or check whether it reaches the end of the file.
Example
Long long;
Fread (& A, sizeof (long), 1, STD );
Char B [10];
Fread (B, sizeof (char), 10, STD );
Question application:
Uvalive 6426-count:
Link: https://icpcarchive.ecs.baylor.edu/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 4437
The question says that the data is read from the binary stream, and the data volume reaches 10000*10000. With scanf, the input plug-in times out, and the input with fread does not time out.
Code:
# Include <iostream> # include <cstdio> # include <algorithm> # include <vector> # include <cstring> using namespace STD; int M, N; int A [10005] [10005]; int main () {int C; fread (& N, sizeof (INT), 1, stdin); fread (& M, sizeof (INT), 1, stdin); For (INT I = 0; I <n; I ++) {fread (A [I], sizeof (INT), m, stdin) ;}int L, R; int last = m; while (fread (& L, sizeof (INT), 1, stdin) {fread (& R, sizeof (INT), 1, stdin); int ans = 0; For (INT I = 0; I <n; I ++) {if (a [I] [0]> r) break; int LL = lower_bound (A [I], a [I] + Last, L)-A [I]; int RR = upper_bound (A [I], a [I] + Last, R)-A [I]; If (RR <= ll) continue; ans + = Rr-ll;} printf ("% d \ n", ANS);} return 0 ;}
Fread Function Application