BIO_S_FILE(3ossl)
π BIO_S_FILE(3ossl) β the FILE bio, but make it make sense π
okay bestie, gather round, because this is the BIO_s_file page from OpenSSL and it's lowkey one of the most useful things in the whole library. it's giving "wrapper energy" and honestly? we're obsessed. πΈ
β¨ NAME β¨
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 β the FILE bio, periodt. π
β¨ SYNOPSIS β¨
#include <openssl/bio.h>
const BIO_METHOD *BIO_s_file(void);
BIO *BIO_new_file(const char *filename, const char *mode);
BIO *BIO_new_fp(FILE *stream, int flags);
long BIO_set_fp(BIO *b, FILE *fp, int flags);
long BIO_get_fp(BIO *b, FILE **fpp);
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);
β¨ DESCRIPTION β¨
so BIO_s_file() hands you the BIO file method. and like the name is literally telling you what it is: it's a wrapper round the stdio FILE structure, and it's a source/sink BIO. no cap. π
calls to BIO_read_ex() and BIO_write_ex() read and write data to the underlying stream. BIO_gets() and BIO_puts() are supported on file BIOs too β slay. β¨
BIO_flush() on a file BIO calls the fflush() function on the wrapped stream.
BIO_reset() attempts to change the file pointer to the start of file using fseek(stream, 0, 0).
BIO_seek() sets the file pointer to position ofs from start of file using fseek(stream, ofs, 0).
BIO_eof() calls feof().
setting the BIO_CLOSE flag calls fclose() on the stream when the BIO is freed. π«Ά
BIO_new_file() creates a new file BIO with mode mode, and the meaning of mode is the same as the stdio function fopen(). the BIO_CLOSE flag is set on the returned BIO. π
BIO_new_fp() creates a file BIO wrapping stream. the flags can be:
BIO_CLOSEBIO_NOCLOSE(this one's the close flag)BIO_FP_TEXT(sets the underlying stream to text mode, default is binary β and heads up, this only has any effect under Win32)
BIO_set_fp() sets the fp of a file BIO to fp. flags has the same meaning as in BIO_new_fp(), and it's a macro.
BIO_get_fp() retrieves the fp of a file BIO, and it's a macro.
BIO_seek() is a macro that sets the position pointer to offset bytes from the start of file.
BIO_tell() returns the value of the position pointer.
BIO_read_filename(), BIO_write_filename(), BIO_append_filename() and BIO_rw_filename() set the file BIO b to use name for reading, writing, append or read write respectively. choose your fighter. π
β¨ NOTES β¨
when you're wrapping stdout, stdin or stderr, the underlying stream should not normally be closed, so the BIO_NOCLOSE flag should be set. do not skip this bestie, i'm literally begging. π
because the file BIO calls the underlying stdio functions, any quirks in stdio behaviour will be mirrored by the corresponding BIO. it's giving "you are what you eat". π
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 filenames in UTF-8. π
β¨ RETURN VALUES β¨
BIO_s_file()returns the file BIO method.BIO_new_file()andBIO_new_fp()return a file BIO orNULLif an error occurred.BIO_set_fp()andBIO_get_fp()return1for success or<=0for failure.BIO_seek()returns0for success or negative values for failure.BIO_tell()returns the current file position or negative values for failure.BIO_read_filename(),BIO_write_filename(),BIO_append_filename()andBIO_rw_filename()return1for success or<=0for failure. an error is also returned if the file does not exist. π¨
β¨ EXAMPLES β¨
file BIO "hello world", the iconic version:
BIO *bio_out;
bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
BIO_printf(bio_out, "Hello World\n");
alternative technique (same vibe, different route):
BIO *bio_out;
bio_out = BIO_new(BIO_s_file());
if (bio_out == NULL)
/* Error */
if (BIO_set_fp(bio_out, stdout, BIO_NOCLOSE) <= 0)
/* Error */
BIO_printf(bio_out, "Hello World\n");
write to a file:
BIO *out;
out = BIO_new_file("filename.txt", "w");
if (!out)
/* Error */
BIO_printf(out, "Hello World\n");
BIO_free(out);
alternative technique, again:
BIO *out;
out = BIO_new(BIO_s_file());
if (out == NULL)
/* Error */
if (BIO_write_filename(out, "filename.txt") <= 0)
/* Error */
BIO_printf(out, "Hello World\n");
BIO_free(out);
β¨ BUGS β¨
BIO_reset() and BIO_seek() are implemented using fseek() on the underlying stream. the return value for fseek() is 0 for success or -1 if an error occurred β and this differs from other types of BIO, which will typically return 1 for success and a non positive value if an error occurred. so yeah, watch out for that one. π not me getting caught by a return convention swap.
β¨ SEE ALSO β¨
BIO_seek(3), BIO_tell(3), BIO_reset(3), BIO_flush(3), BIO_read_ex(3), BIO_write_ex(3), BIO_puts(3), BIO_gets(3), BIO_printf(3), BIO_set_close(3), BIO_get_close(3)
β¨ COPYRIGHT β¨
Copyright 2000-2026 The OpenSSL Project Authors. All Rights Reserved.
Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.
okay that's the whole thing, you ate it up fr fr π this was OpenSSL 3.5.9, dated 2026-09-29. go wrap a FILE and be the main character of your own I/O. bye bestie, ily πβ¨π¦
π¦ where this page comes from
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms