{"id":7348,"date":"2022-12-20T19:37:46","date_gmt":"2022-12-20T22:37:46","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/fread-man3\/"},"modified":"2022-12-20T19:37:46","modified_gmt":"2022-12-20T22:37:46","slug":"fread-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/fread-man3\/","title":{"rendered":"FREAD (man3)"},"content":{"rendered":"<h1 align=\"center\">FREAD<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#RETURN VALUE\">RETURN VALUE<\/a><br \/> <a href=\"#ATTRIBUTES\">ATTRIBUTES<\/a><br \/> <a href=\"#CONFORMING TO\">CONFORMING TO<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COLOPHON\">COLOPHON<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">fread, fwrite \u2212 binary stream input\/output<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>#include <stdio.h><\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>size_t fread(void *<\/b><i>ptr<\/i><b>, size_t<\/b> <i>size<\/i><b>, size_t<\/b> <i>nmemb<\/i><b>, FILE *<\/b><i>stream<\/i><b>);<\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>size_t fwrite(const void *<\/b><i>ptr<\/i><b>, size_t<\/b> <i>size<\/i><b>, size_t<\/b> <i>nmemb<\/i><b>, <br \/> FILE *<\/b><i>stream<\/i><b>);<\/b><\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The function <b>fread<\/b>() reads <i>nmemb<\/i> items of data, each <i>size<\/i> bytes long, from the stream pointed to by <i>stream<\/i>, storing them at the location given by <i>ptr<\/i>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The function <b>fwrite<\/b>() writes <i>nmemb<\/i> items of data, each <i>size<\/i> bytes long, to the stream pointed to by <i>stream<\/i>, obtaining them from the location given by <i>ptr<\/i>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">For nonlocking counterparts, see <b>unlocked_stdio<\/b>(3).<\/p>\n<h2>RETURN VALUE <a name=\"RETURN VALUE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">On success, <b>fread<\/b>() and <b>fwrite<\/b>() return the number of items read or written. This number equals the number of bytes transferred only when <i>size<\/i> is 1. If an error occurs, or the end of the file is reached, the return value is a short item count (or zero).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The file position indicator for the stream is advanced by the number of bytes successfully read or written.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>fread<\/b>() does not distinguish between end-of-file and error, and callers must use <b>feof<\/b>(3) and <b>ferror<\/b>(3) to determine which occurred.<\/p>\n<h2>ATTRIBUTES <a name=\"ATTRIBUTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">For an explanation of the terms used in this section, see <b>attributes<\/b>(7).<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"8%\"><\/td>\n<td width=\"7%\"> <\/td>\n<td width=\"8%\"> <\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"8%\"> <\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<\/table>\n<p align=\"center\"><img decoding=\"async\" src=\"grohtml-1642941.png\" alt=\"Image grohtml-1642941.png\"><\/p>\n<h2>CONFORMING TO <a name=\"CONFORMING TO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">POSIX.1-2001, POSIX.1-2008, C89.<\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The program below demonstrates the use of <b>fread<\/b>() by parsing \/bin\/sh ELF executable in binary mode and printing its magic and class:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">$ <b>.\/a.out<\/b> <br \/> ELF magic: 0x7f454c46 <br \/> Class: 0x02<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Program source<\/b> <br \/> #include <stdio.h> <br \/> #include <stdlib.h><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">#define ARRAY_SIZE(arr) (sizeof(arr) \/ sizeof((arr)[0]))<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">int <br \/> main(void) <br \/> { <br \/> FILE *fp = fopen(&#8220;\/bin\/sh&#8221;, &#8220;rb&#8221;); <br \/> if (!fp) { <br \/> perror(&#8220;fopen&#8221;); <br \/> return EXIT_FAILURE; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">unsigned char buffer[4];<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">size_t ret = fread(buffer, ARRAY_SIZE(buffer), sizeof(*buffer), fp); <br \/> if (ret != sizeof(*buffer)) { <br \/> fprintf(stderr, &#8220;fread() failed: %zun&#8221;, ret); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">printf(&#8220;ELF magic: %#04x%02x%02x%02xn&#8221;, buffer[0], buffer[1], <br \/> buffer[2], buffer[3]);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">ret = fread(buffer, 1, 1, fp); <br \/> if (ret != 1) { <br \/> fprintf(stderr, &#8220;fread() failed: %zun&#8221;, ret); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">printf(&#8220;Class: %#04xn&#8221;, buffer[0]);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">fclose(fp);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">exit(EXIT_SUCCESS); <br \/> }<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>read<\/b>(2), <b>write<\/b>(2), <b>feof<\/b>(3), <b>ferror<\/b>(3), <b>unlocked_stdio<\/b>(3)<\/p>\n<h2>COLOPHON <a name=\"COLOPHON\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This page is part of release 5.10 of the Linux <i>man-pages<\/i> project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https:\/\/www.kernel.org\/doc\/man\u2212pages\/.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  fread, fwrite \u2212 binary stream input\/output <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2536],"tags":[2538,2106,3007],"class_list":["post-7348","post","type-post","status-publish","format-standard","hentry","category-3-llamadas-de-bibliotecas","tag-2538","tag-fread","tag-man3"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7348","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=7348"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7348\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=7348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=7348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=7348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}