Fread Function Application and fread Function Application

Source: Internet
Author: User
Tags fread

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.