
#use wml::openssl-macros area=docs page=BIO_s_file

<title>Documents, BIO_s_file(3)</title>

<h1>BIO_s_file(3)</h1>

#use wml::imp::generic

{:
## What's this? [[s|(<STRONG>[^<].+?)</A>(</STRONG><DD>)|$1$2|sg]]
[[s|<P>\s+<P>|<P>|sg]]
[[s|<P>\s+</|</|sg]]
[[s|<DD>\s*<DT>|<DD>&nbsp;<DT>|sg]]
[[s|<DD>\s*</DL>|<DD>&nbsp;</DL>|sg]]
[[s|\[|&#91;|sg]]
[[s|\]|&#93;|sg]]

<!-- INDEX BEGIN -->

<UL>

	<LI><A HREF="#NAME">NAME</A>
	<LI><A HREF="#SYNOPSIS">SYNOPSIS</A>
	<LI><A HREF="#DESCRIPTION">DESCRIPTION</A>
	<LI><A HREF="#NOTES">NOTES</A>
	<LI><A HREF="#EXAMPLES">EXAMPLES</A>
	<LI><A HREF="#RETURN_VALUES">RETURN VALUES</A>
	<LI><A HREF="#BUGS">BUGS</A>
	<LI><A HREF="#SEE_ALSO">SEE ALSO</A>
</UL>
<!-- INDEX END -->

<HR>
<P>
<HR>
<H1><A NAME="NAME">NAME</A></H1>
<P>
BIO_s_file, BIO_new_file, BIO_new_fp, BIO_set_fp, BIO_get_fp,
BIO_read_filename, BIO_write_filename, BIO_append_filename, BIO_rw_filename
- FILE bio

</P>
<P>
<HR>
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
<PRE> #include &lt;openssl/bio.h&gt;
</PRE>
<PRE> BIO_METHOD *   BIO_s_file(void);
 BIO *BIO_new_file(const char *filename, const char *mode);
 BIO *BIO_new_fp(FILE *stream, int flags);
</PRE>
<PRE> BIO_set_fp(BIO *b,FILE *fp, int flags);
 BIO_get_fp(BIO *b,FILE **fpp);
</PRE>
<PRE> int BIO_read_filename(BIO *b, char *name)
 int BIO_write_filename(BIO *b, char *name)
 int BIO_append_filename(BIO *b, char *name)
 int BIO_rw_filename(BIO *b, char *name)
</PRE>
<P>
<HR>
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
<P>
<CODE>BIO_s_file()</CODE> returns the BIO file method. As its name implies
it is a wrapper round the stdio FILE structure and it is a source/sink BIO.

</P>
<P>
Calls to <CODE>BIO_read()</CODE> and <CODE>BIO_write()</CODE> read and
write data to the underlying stream. <CODE>BIO_gets()</CODE> and
<CODE>BIO_puts()</CODE> are supported on file BIOs.

</P>
<P>
<CODE>BIO_flush()</CODE> on a file BIO calls the <CODE>fflush()</CODE>
function on the wrapped stream.

</P>
<P>
<CODE>BIO_reset()</CODE> attempts to change the file pointer to the start
of file using <CODE>fseek(stream,</CODE> 0, 0).

</P>
<P>
<CODE>BIO_seek()</CODE> sets the file pointer to position <STRONG>ofs</STRONG> from start of file using <CODE>fseek(stream,</CODE> ofs, 0).

</P>
<P>
<CODE>BIO_eof()</CODE> calls <CODE>feof().</CODE>

</P>
<P>
Setting the BIO_CLOSE flag calls <CODE>fclose()</CODE> on the stream when
the BIO is freed.

</P>
<P>
<CODE>BIO_new_file()</CODE> creates a new file BIO with mode <STRONG>mode</STRONG> the meaning of <STRONG>mode</STRONG> is the same as the stdio function <CODE>fopen().</CODE> The BIO_CLOSE flag
is set on the returned BIO.

</P>
<P>
<CODE>BIO_new_fp()</CODE> creates a file BIO wrapping <STRONG>stream</STRONG>. Flags can be: BIO_CLOSE, BIO_NOCLOSE (the close flag) BIO_FP_TEXT (sets
the underlying stream to text mode, default is binary: this only has any
effect under Win32).

</P>
<P>
<CODE>BIO_set_fp()</CODE> set the fp of a file BIO to <STRONG>fp</STRONG>. <STRONG>flags</STRONG> has the same meaning as in <CODE>BIO_new_fp(),</CODE> it is a macro.

</P>
<P>
<CODE>BIO_get_fp()</CODE> retrieves the fp of a file BIO, it is a macro.

</P>
<P>
<CODE>BIO_seek()</CODE> is a macro that sets the position pointer to <STRONG>offset</STRONG> bytes from the start of file.

</P>
<P>
<CODE>BIO_tell()</CODE> returns the value of the position pointer.

</P>
<P>
<CODE>BIO_read_filename(),</CODE> <CODE>BIO_write_filename(),</CODE>
<CODE>BIO_append_filename()</CODE> and <CODE>BIO_rw_filename()</CODE> set
the file BIO <STRONG>b</STRONG> to use file <STRONG>name</STRONG> for reading, writing, append or read write respectively.

</P>
<P>
<HR>
<H1><A NAME="NOTES">NOTES</A></H1>
<P>
When wrapping stdout, stdin or stderr the underlying stream should not
normally be closed so the BIO_NOCLOSE flag should be set.

</P>
<P>
Because the file BIO calls the underlying stdio functions any quirks in
stdio behaviour will be mirrored by the corresponding BIO.

</P>
<P>
On Windows BIO_new_files reserves for the filename argument to be UTF-8
encoded. In other words if you have to make it work in multi- lingual
environment, encode file names in UTF-8.

</P>
<P>
<HR>
<H1><A NAME="EXAMPLES">EXAMPLES</A></H1>
<P>
File BIO ``hello world'':

</P>
<PRE> BIO *bio_out;
 bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
 BIO_printf(bio_out, &quot;Hello World\n&quot;);
</PRE>
<P>
Alternative technique:

</P>
<PRE> BIO *bio_out;
 bio_out = BIO_new(BIO_s_file());
 if(bio_out == NULL) /* Error ... */
 if(!BIO_set_fp(bio_out, stdout, BIO_NOCLOSE)) /* Error ... */
 BIO_printf(bio_out, &quot;Hello World\n&quot;);
</PRE>
<P>
Write to a file:

</P>
<PRE> BIO *out;
 out = BIO_new_file(&quot;filename.txt&quot;, &quot;w&quot;);
 if(!out) /* Error occurred */
 BIO_printf(out, &quot;Hello World\n&quot;);
 BIO_free(out);
</PRE>
<P>
Alternative technique:

</P>
<PRE> BIO *out;
 out = BIO_new(BIO_s_file());
 if(out == NULL) /* Error ... */
 if(!BIO_write_filename(out, &quot;filename.txt&quot;)) /* Error ... */
 BIO_printf(out, &quot;Hello World\n&quot;);
 BIO_free(out);
</PRE>
<P>
<HR>
<H1><A NAME="RETURN_VALUES">RETURN VALUES</A></H1>
<P>
<CODE>BIO_s_file()</CODE> returns the file BIO method.

</P>
<P>
<CODE>BIO_new_file()</CODE> and <CODE>BIO_new_fp()</CODE> return a file BIO
or NULL if an error occurred.

</P>
<P>
<CODE>BIO_set_fp()</CODE> and <CODE>BIO_get_fp()</CODE> return 1 for
success or 0 for failure (although the current implementation never return
0).

</P>
<P>
<CODE>BIO_seek()</CODE> returns the same value as the underlying
<CODE>fseek()</CODE> function: 0 for success or -1 for failure.

</P>
<P>
<CODE>BIO_tell()</CODE> returns the current file position.

</P>
<P>
<CODE>BIO_read_filename(),</CODE> <CODE>BIO_write_filename(),</CODE>
<CODE>BIO_append_filename()</CODE> and <CODE>BIO_rw_filename()</CODE>
return 1 for success or 0 for failure.

</P>
<P>
<HR>
<H1><A NAME="BUGS">BUGS</A></H1>
<P>
<CODE>BIO_reset()</CODE> and <CODE>BIO_seek()</CODE> are implemented using
<CODE>fseek()</CODE> on the underlying stream. The return value for
<CODE>fseek()</CODE> is 0 for success or -1 if an error occurred this
differs from other types of BIO which will typically return 1 for success
and a non positive value if an error occurred.

</P>
<P>
<HR>
<H1><A NAME="SEE_ALSO">SEE ALSO</A></H1>
<P>
<A HREF="../crypto/BIO_ctrl.html#">BIO_seek(3)</A>, <A HREF="../crypto/BIO_ctrl.html#">BIO_tell(3)</A>,
<A HREF="../crypto/BIO_ctrl.html#">BIO_reset(3)</A>, <A HREF="../crypto/BIO_ctrl.html#">BIO_flush(3)</A>,
<A HREF="../crypto/BIO_read.html#">BIO_read(3)</A>,
<A HREF="../crypto/BIO_read.html#">BIO_write(3)</A>, <A HREF="../crypto/BIO_read.html#">BIO_puts(3)</A>,
<A HREF="../crypto/BIO_read.html#">BIO_gets(3)</A>, <EM>BIO_printf(3)</EM>,
<A HREF="../crypto/BIO_ctrl.html#">BIO_set_close(3)</A>, <A HREF="../crypto/BIO_ctrl.html#">BIO_get_close(3)</A>

</P>
:}

