Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the fread function in C language

Shulou Source: shulou.com Published: 2022-05-31 10:56:08 09月23日 Update

This article introduces the relevant knowledge of "how to use the fread function in c language". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. The C language fread function is used to read the data in the file into the specified buffer.

2. The fread function does not distinguish between the tail of the file and the error, so the caller must use feof and ferror to determine what happened.

If an error or reaches the end of the file, the number of elements returned for successful reading may be less than nmemb.

Example

# include # include int main () {FILE * fp; char c [] = "www.codersrc.com"; char buffer [2048] = {0}; / * Open the file for reading and writing * / fp = fopen ("file.txt", "w +"); / * write data to the file * / fwrite (c, strlen (c) + 1, 1, fp) / * offset to the beginning of the file * / fseek (fp, 0, SEEK_SET); / * read and display data * / fread (buffer,1, strlen (c) + 1, fp); printf ("% s\ n", buffer); fclose (fp); return (0);} "how to use fread functions in c language" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: File function language data content more knowledge error utility success achievement next element dilemma instance actual tail beginning situation quantity Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Information Redmi NVidia Microsoft