1<html lang="en"> 2<head> 3<title>fgets - Untitled</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Untitled"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Stdio.html#Stdio" title="Stdio"> 9<link rel="prev" href="fgetpos.html#fgetpos" title="fgetpos"> 10<link rel="next" href="fgetwc.html#fgetwc" title="fgetwc"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<meta http-equiv="Content-Style-Type" content="text/css"> 13<style type="text/css"><!-- 14 pre.display { font-family:inherit } 15 pre.format { font-family:inherit } 16 pre.smalldisplay { font-family:inherit; font-size:smaller } 17 pre.smallformat { font-family:inherit; font-size:smaller } 18 pre.smallexample { font-size:smaller } 19 pre.smalllisp { font-size:smaller } 20 span.sc { font-variant:small-caps } 21 span.roman { font-family:serif; font-weight:normal; } 22 span.sansserif { font-family:sans-serif; font-weight:normal; } 23--></style> 24<link rel="stylesheet" type="text/css" href="../cs.css"> 25</head> 26<body> 27<div class="node"> 28<a name="fgets"></a> 29<p> 30Next: <a rel="next" accesskey="n" href="fgetwc.html#fgetwc">fgetwc</a>, 31Previous: <a rel="previous" accesskey="p" href="fgetpos.html#fgetpos">fgetpos</a>, 32Up: <a rel="up" accesskey="u" href="Stdio.html#Stdio">Stdio</a> 33<hr> 34</div> 35 36<h3 class="section">4.12 <code>fgets</code>—get character string from a file or stream</h3> 37 38<p><a name="index-fgets-168"></a><a name="index-g_t_005ffgets_005fr-169"></a><strong>Synopsis</strong> 39<pre class="example"> #include <stdio.h> 40 char *fgets(char *<var>buf</var>, int <var>n</var>, FILE *<var>fp</var>); 41 42 #include <stdio.h> 43 char *_fgets_r(struct _reent *<var>ptr</var>, char *<var>buf</var>, int <var>n</var>, FILE *<var>fp</var>); 44 45</pre> 46 <p><strong>Description</strong><br> 47Reads at most <var>n-1</var> characters from <var>fp</var> until a newline 48is found. The characters including to the newline are stored 49in <var>buf</var>. The buffer is terminated with a 0. 50 51 <p>The <code>_fgets_r</code> function is simply the reentrant version of 52<code>fgets</code> and is passed an additional reentrancy structure 53pointer: <var>ptr</var>. 54 55 <p><br> 56<strong>Returns</strong><br> 57<code>fgets</code> returns the buffer passed to it, with the data 58filled in. If end of file occurs with some data already 59accumulated, the data is returned with no other indication. If 60no data are read, NULL is returned instead. 61 62 <p><br> 63<strong>Portability</strong><br> 64<code>fgets</code> should replace all uses of <code>gets</code>. Note however 65that <code>fgets</code> returns all of the data, while <code>gets</code> removes 66the trailing newline (with no indication that it has done so.) 67 68 <p>Supporting OS subroutines required: <code>close</code>, <code>fstat</code>, <code>isatty</code>, 69<code>lseek</code>, <code>read</code>, <code>sbrk</code>, <code>write</code>. 70 71 <p><br> 72 73 </body></html> 74 75