Files
oldlinux-files/Ref-docs/POSIX/susv3/functions/fread.html
2024-02-19 00:21:47 -05:00

176 lines
7.3 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
<title>fread</title>
</head>
<body bgcolor="white">
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
</script>
<basefont size="3"> <a name="fread"></a> <a name="tag_03_185"></a><!-- fread -->
<!--header start-->
<center><font size="2">The Open Group Base Specifications Issue 6<br>
IEEE Std 1003.1-2001<br>
Copyright &copy; 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
<!--header end-->
<hr size="2" noshade>
<h4><a name="tag_03_185_01"></a>NAME</h4>
<blockquote>fread - binary input</blockquote>
<h4><a name="tag_03_185_02"></a>SYNOPSIS</h4>
<blockquote class="synopsis">
<p><code><tt>#include &lt;<a href="../basedefs/stdio.h.html">stdio.h</a>&gt;<br>
<br>
size_t fread(void *restrict</tt> <i>ptr</i><tt>, size_t</tt> <i>size</i><tt>, size_t</tt> <i>nitems</i><tt>,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FILE *restrict</tt> <i>stream</i><tt>);<br>
</tt></code></p>
</blockquote>
<h4><a name="tag_03_185_03"></a>DESCRIPTION</h4>
<blockquote>
<div class="box"><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]"
border="0"> The functionality described on this reference page is aligned with the ISO&nbsp;C standard. Any conflict between the
requirements described here and the ISO&nbsp;C standard is unintentional. This volume of IEEE&nbsp;Std&nbsp;1003.1-2001 defers to
the ISO&nbsp;C standard. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></div>
<p>The <i>fread</i>() function shall read into the array pointed to by <i>ptr</i> up to <i>nitems</i> elements whose size is
specified by <i>size</i> in bytes, from the stream pointed to by <i>stream</i>. For each object, <i>size</i> calls shall be made to
the <a href="../functions/fgetc.html"><i>fgetc</i>()</a> function and the results stored, in the order read, in an array of
<b>unsigned char</b> exactly overlaying the object. The file position indicator for the stream (if defined) shall be advanced by
the number of bytes successfully read. If an error occurs, the resulting value of the file position indicator for the stream is
unspecified. If a partial element is read, its value is unspecified.</p>
<p><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> The
<i>fread</i>() function may mark the <i>st_atime</i> field of the file associated with <i>stream</i> for update. The
<i>st_atime</i> field shall be marked for update by the first successful execution of <a href=
"../functions/fgetc.html"><i>fgetc</i>()</a>, <a href="../functions/fgets.html"><i>fgets</i>()</a>, <a href=
"../functions/fgetwc.html"><i>fgetwc</i>()</a>, <a href="../functions/fgetws.html"><i>fgetws</i>()</a>, <i>fread</i>(), <a href=
"../functions/fscanf.html"><i>fscanf</i>()</a>, <a href="../functions/getc.html"><i>getc</i>()</a>, <a href=
"../functions/getchar.html"><i>getchar</i>()</a>, <a href="../functions/gets.html"><i>gets</i>()</a>, or <a href=
"../functions/scanf.html"><i>scanf</i>()</a> using <i>stream</i> that returns data not supplied by a prior call to <a href=
"../functions/ungetc.html"><i>ungetc</i>()</a> or <a href="../functions/ungetwc.html"><i>ungetwc</i>()</a>. <img src=
"../images/opt-end.gif" alt="[Option End]" border="0"></p>
</blockquote>
<h4><a name="tag_03_185_04"></a>RETURN VALUE</h4>
<blockquote>
<p>Upon successful completion, <i>fread</i>() shall return the number of elements successfully read which is less than
<i>nitems</i> only if a read error or end-of-file is encountered. If <i>size</i> or <i>nitems</i> is 0, <i>fread</i>() shall return
0 and the contents of the array and the state of the stream remain unchanged. Otherwise, if a read error occurs, the error
indicator for the stream shall be set, <sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif"
alt="[Option Start]" border="0"> &nbsp;and <i>errno</i> shall be set to indicate the error. <img src="../images/opt-end.gif" alt=
"[Option End]" border="0"></p>
</blockquote>
<h4><a name="tag_03_185_05"></a>ERRORS</h4>
<blockquote>
<p>Refer to <a href="fgetc.html"><i>fgetc</i>()</a> .</p>
</blockquote>
<hr>
<div class="box"><em>The following sections are informative.</em></div>
<h4><a name="tag_03_185_06"></a>EXAMPLES</h4>
<blockquote>
<h5><a name="tag_03_185_06_01"></a>Reading from a Stream</h5>
<p>The following example reads a single element from the <i>fp</i> stream into the array pointed to by <i>buf</i>.</p>
<pre>
<tt>#include &lt;stdio.h&gt;
...
size_t bytes_read;
char buf[100];
FILE *fp;
...
bytes_read = fread(buf, sizeof(buf), 1, fp);
...
</tt>
</pre>
</blockquote>
<h4><a name="tag_03_185_07"></a>APPLICATION USAGE</h4>
<blockquote>
<p>The <a href="../functions/ferror.html"><i>ferror</i>()</a> or <a href="../functions/feof.html"><i>feof</i>()</a> functions must
be used to distinguish between an error condition and an end-of-file condition.</p>
<p>Because of possible differences in element length and byte ordering, files written using <a href=
"../functions/fwrite.html"><i>fwrite</i>()</a> are application-dependent, and possibly cannot be read using <i>fread</i>() by a
different application or by the same application on a different processor.</p>
</blockquote>
<h4><a name="tag_03_185_08"></a>RATIONALE</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_185_09"></a>FUTURE DIRECTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_185_10"></a>SEE ALSO</h4>
<blockquote>
<p><a href="feof.html"><i>feof</i>()</a> , <a href="ferror.html"><i>ferror</i>()</a> , <a href="fgetc.html"><i>fgetc</i>()</a> , <a
href="fopen.html"><i>fopen</i>()</a> , <a href="getc.html"><i>getc</i>()</a> , <a href="gets.html"><i>gets</i>()</a> , <a href=
"scanf.html"><i>scanf</i>()</a> , the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href=
"../basedefs/stdio.h.html"><i>&lt;stdio.h&gt;</i></a></p>
</blockquote>
<h4><a name="tag_03_185_11"></a>CHANGE HISTORY</h4>
<blockquote>
<p>First released in Issue 1. Derived from Issue 1 of the SVID.</p>
</blockquote>
<h4><a name="tag_03_185_12"></a>Issue 6</h4>
<blockquote>
<p>Extensions beyond the ISO&nbsp;C standard are marked.</p>
<p>The following changes are made for alignment with the ISO/IEC&nbsp;9899:1999 standard:</p>
<ul>
<li>
<p>The <i>fread</i>() prototype is updated.</p>
</li>
<li>
<p>The DESCRIPTION is updated to describe how the bytes from a call to <a href="../functions/fgetc.html"><i>fgetc</i>()</a> are
stored.</p>
</li>
</ul>
</blockquote>
<div class="box"><em>End of informative text.</em></div>
<hr>
<hr size="2" noshade>
<center><font size="2"><!--footer start-->
UNIX &reg; is a registered Trademark of The Open Group.<br>
POSIX &reg; is a registered Trademark of The IEEE.<br>
[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
"../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>
]</font></center>
<!--footer end-->
<hr size="2" noshade>
</body>
</html>