Fread Function Application and fread Function Application
Header file: <cstdio/stdio. h>
Purpose: read 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 size-byte data items to be read and written. Each element is size-byte.
Stream: input stream
Returned value: the number of actually read elements. If the returned value is different from the count value, the end of the file or an error may occur.
Get error information from ferror and feof or check whether it has reached the end of the file.
Example
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;}
Use of fread Functions
Will it be that I only allocated space for p, but not for p-> next?
Use of fseek and fread functions for File Reading
The width of the 18-byte read from the beginning of the file is 2 bytes in length.
Then read the height from 2 bytes after the width.