MicrOS
stdio.h File Reference
#include <stdint.h>
#include <stdarg.h>
#include "stdlib.h"

Go to the source code of this file.

Classes

struct  FILE
 Object containing information to control a stream. More...
 

Macros

#define BUFSIZ   1024 * 32
 Size of the stream buffer. More...
 
#define EOF   -1
 Value returned when internal stream position has reached the end of file. More...
 
#define FILENAME_MAX   255
 Maximum length of file names. More...
 
#define FOPEN_MAX   INT32_MAX
 Potential limit of simultaneous open streams. More...
 
#define L_tmpnam   0
 Minimum length for temporary file name. More...
 
#define TMP_MAX   0
 Maximum number of temporary files. More...
 
#define SEEK_SET   0
 Seek-Set mode (position is absolute) More...
 
#define SEEK_CUR   1
 Seek-Current mode (relative to the current position) More...
 
#define SEEK_END   2
 Seek-End mode (relative to the end of the file) More...
 
#define _IONBF   0
 No Buffering mode. More...
 
#define _IOLBF   1
 Line Buffering mode. More...
 
#define _IOFBF   2
 Full Buffering mode. More...
 

Typedefs

typedef uint32_t size_t
 Unsigned integral type. More...
 
typedef uint32_t fpos_t
 Object containing information to specify a position within a file. More...
 

Enumerations

enum  file_buffering_mode { file_buffering_mode_none = 0, file_buffering_mode_line = 1, file_buffering_mode_full = 2 }
 Stream buffering modest. More...
 
enum  file_mode {
  file_mode_none, file_mode_read, file_mode_write, file_mode_append,
  file_mode_read_and_update, file_mode_write_and_update, file_mode_append_and_update
}
 

Functions

int remove (const char *filename)
 Remove file. More...
 
int rename (const char *oldname, const char *newname)
 Rename file. More...
 
FILEfopen (const char *filename, const char *mode)
 Open file. More...
 
FILEfreopen (const char *filename, const char *mode, FILE *stream)
 Reopen stream with different file or mode. More...
 
int fclose (FILE *stream)
 Close file. More...
 
int fflush (FILE *stream)
 Flush stream. More...
 
void setbuf (FILE *stream, char *buffer)
 Set stream buffer. More...
 
int setvbuf (FILE *stream, char *buffer, int mode, size_t size)
 Change stream buffering. More...
 
int fgetc (FILE *stream)
 Get character from stream. More...
 
char * fgets (char *str, int num, FILE *stream)
 Get string from stream. More...
 
int fputc (int character, FILE *stream)
 Write character to stream. More...
 
int fputs (const char *str, FILE *stream)
 Write string to stream. More...
 
int getc (FILE *stream)
 Get character from stream. More...
 
int getchar ()
 Get character from stdin. More...
 
char * gets (char *str)
 Get string from stdin. More...
 
int putc (int character, FILE *stream)
 Write character to stream. More...
 
int putchar (int character)
 Write character to stdout. More...
 
int puts (const char *str)
 Write string to stdout. More...
 
int ungetc (int character, FILE *stream)
 Unget character from stream. More...
 
size_t fread (void *ptr, size_t size, size_t count, FILE *stream)
 Read block of data from stream. More...
 
size_t fwrite (const void *ptr, size_t size, size_t count, FILE *stream)
 Write block of data to stream. More...
 
int fgetpos (FILE *stream, fpos_t *pos)
 Get current position in stream. More...
 
int fseek (FILE *stream, long int offset, int origin)
 Reposition stream position indicator. More...
 
int fsetpos (FILE *stream, const fpos_t *pos)
 Set position indicator of stream. More...
 
long int ftell (FILE *stream)
 Get current position in stream. More...
 
void rewind (FILE *stream)
 Set position of stream to the beginning. More...
 
void clearerr (FILE *stream)
 Clear error indicators. More...
 
int feof (FILE *stream)
 Check end-of-file indicator. More...
 
int ferror (FILE *stream)
 Check error indicator. More...
 
void perror (const char *str)
 Print error message. More...
 
int sprintf (char *str, const char *format,...)
 Write formatted data to string. More...
 
int printf (const char *format,...)
 Print formatted data to stdout. More...
 
int fprintf (FILE *file, const char *format,...)
 Print formatted data to stdoutWrite formatted data to stream. More...
 
int vfprintf (FILE *stream, const char *format, va_list arg)
 Print formatted data from variable argument list to stream. More...
 
int vprintf (const char *format, va_list arg)
 Print formatted data from variable argument list to stdout. More...
 
int vsprintf (char *str, const char *format, va_list arg)
 Print formatted data from variable argument list to string. More...
 
int vfscanf (FILE *stream, const char *format, va_list arg)
 Read formatted data from stream into variable argument list. More...
 
int scanf (const char *format,...)
 Read formatted data from stdin. More...
 
FILE__stdio_create_stream ()
 Creates new stream. More...
 
file_mode __stdio_get_file_mode (const char *str_mode)
 Parses file mode. More...
 

Variables

FILEstdin
 Standard input (default is keyboard). More...
 
FILEstdout
 Standard output (default is console). More...
 
FILEstderr
 Standard error output (default is console). More...
 

Macro Definition Documentation

◆ _IOFBF

#define _IOFBF   2

Full Buffering mode.

◆ _IOLBF

#define _IOLBF   1

Line Buffering mode.

◆ _IONBF

#define _IONBF   0

No Buffering mode.

◆ BUFSIZ

#define BUFSIZ   1024 * 32

Size of the stream buffer.

◆ EOF

#define EOF   -1

Value returned when internal stream position has reached the end of file.

◆ FILENAME_MAX

#define FILENAME_MAX   255

Maximum length of file names.

◆ FOPEN_MAX

#define FOPEN_MAX   INT32_MAX

Potential limit of simultaneous open streams.

◆ L_tmpnam

#define L_tmpnam   0

Minimum length for temporary file name.

◆ SEEK_CUR

#define SEEK_CUR   1

Seek-Current mode (relative to the current position)

◆ SEEK_END

#define SEEK_END   2

Seek-End mode (relative to the end of the file)

◆ SEEK_SET

#define SEEK_SET   0

Seek-Set mode (position is absolute)

◆ TMP_MAX

#define TMP_MAX   0

Maximum number of temporary files.

Typedef Documentation

◆ fpos_t

typedef uint32_t fpos_t

Object containing information to specify a position within a file.

◆ size_t

typedef uint32_t size_t

Unsigned integral type.

Enumeration Type Documentation

◆ file_buffering_mode

Stream buffering modest.

Enumerator
file_buffering_mode_none 

Flush when new data is present (don't store it in buffer).

file_buffering_mode_line 

Flush if buffer is full, new line char is present or fflush has been called.

file_buffering_mode_full 

Flush if buffer is full or fflush has been called.

◆ file_mode

enum file_mode
Enumerator
file_mode_none 
file_mode_read 
file_mode_write 
file_mode_append 
file_mode_read_and_update 
file_mode_write_and_update 
file_mode_append_and_update 

Function Documentation

◆ __stdio_create_stream()

FILE* __stdio_create_stream ( )

Creates new stream.

Creates new stream (not assigned to any file or device) and returns it.

Returns
A newly created stream.

◆ __stdio_get_file_mode()

file_mode __stdio_get_file_mode ( const char *  str_mode)

Parses file mode.

Parses file mode in string format (eg. r+) and returns corresponding enum.

Returns
File mode in enum representation.

◆ clearerr()

void clearerr ( FILE stream)

Clear error indicators.

Resets both the error and the eof indicators of the stream.

Parameters
streamPointer to a FILE object that identifies the stream to be closed.

◆ fclose()

int fclose ( FILE stream)

Close file.

Closes the file associated with the stream and disassociates it.

Parameters
streamPointer to a FILE object that identifies the stream to be closed.
Returns
If the stream is successfully closed, a zero value is returned. On failure, EOF is returned.

◆ feof()

int feof ( FILE stream)

Check end-of-file indicator.

Checks whether the end-of-File indicator associated with stream is set, returning a value different from zero if it is.

Parameters
streamstream Pointer to a FILE object that identifies the stream to be closed.
Returns
A non-zero value is returned in the case that the end-of-file indicator associated with the stream is set. Otherwise, zero is returned.

◆ ferror()

int ferror ( FILE stream)

Check error indicator.

Checks if the error indicator associated with stream is set, returning a value different from zero if it is.

Parameters
sstreamtream Pointer to a FILE object that identifies the stream to be closed.
Returns
A non-zero value is returned in the case that the error indicator associated with the stream is set. Otherwise, zero is returned.

◆ fflush()

int fflush ( FILE stream)

Flush stream.

If the given stream was open for writing any unwritten data in its output buffer is written to the file. If stream is a null pointer, all such streams are flushed.

Parameters
streamPointer to a FILE object that identifies the stream to be closed.
Returns
A zero value indicates success. If an error occurs, EOF is returned and the error indicator is set.

◆ fgetc()

int fgetc ( FILE stream)

Get character from stream.

Returns the character currently pointed by the internal file position indicator of the specified stream. The internal file position indicator is then advanced to the next character.

Parameters
streamPointer to a FILE object that identifies the stream to be closed.
Returns
On success, the character read is returned. If the position indicator was at the end-of-file, the function returns EOF.

◆ fgetpos()

int fgetpos ( FILE stream,
fpos_t pos 
)

Get current position in stream.

Retrieves the current position in the stream.

Parameters
streamPointer to a FILE object that identifies the stream to be closed.
posPointer to a fpos_t object. This should point to an object already allocated.
Returns
On success, the function returns zero. In case of error, errno is set to a platform-specific positive value and the function returns a non-zero value.

◆ fgets()

char* fgets ( char *  str,
int  num,
FILE stream 
)

Get string from stream.

Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first.

Parameters
strPointer to an array of chars where the string read is copied.
numMaximum number of characters to be copied into str (including the terminating null-character).
streamPointer to a FILE object that identifies the stream to be closed.
Returns
On success, the function returns str. If the end-of-file is encountered while attempting to read a character, the eof indicator is set. If this happens before any characters could be read, the pointer returned is a null pointer.

◆ fopen()

FILE* fopen ( const char *  filename,
const char *  mode 
)

Open file.

Opens the file whose name is specified in the parameter filename and associates it with a stream that can be identified in future operations by the FILE pointer returned. The returned stream is fully buffered by default.

Parameters
filenameC string containing the name of the file to be opened.
modeC string containing a file access mode (r, w, a, r+, w+, a+).
Returns
If the file is successfully opened, the function returns a pointer to a FILE object that can be used to identify the stream on future operations. Otherwise, a null pointer is returned.

◆ fprintf()

int fprintf ( FILE file,
const char *  format,
  ... 
)

Print formatted data to stdoutWrite formatted data to stream.

Writes the C string pointed by format to the stream. If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers.

Parameters
filePointer to a FILE object that identifies an output stream.
formatC string that contains the text to be written to the stream.Can contain embedded format specifiers.
...Additional arguments
Returns
Total number of characters written to stream.

◆ fputc()

int fputc ( int  character,
FILE stream 
)

Write character to stream.

Writes a character to the stream and advances the position indicator.

Parameters
characterThe int promotion of the character to be written.
streamPointer to a FILE object that identifies the stream to be closed.
Returns
On success, the character written is returned. If a writing error occurs, EOF is returned and the error indicator (ferror) is set.

◆ fputs()

int fputs ( const char *  str,
FILE stream 
)

Write string to stream.

Writes the C string pointed by str to the stream. The function begins copying from the address specified (str) until it reaches the terminating null character ('\0'). This terminating null-character is not copied to the stream.

Parameters
strC string with the content to be written to stream.
streamPointer to a FILE object that identifies the stream to be closed.
Returns
On success, a non-negative value is returned. On error, the function returns EOF and sets the error indicator.

◆ fread()

size_t fread ( void *  ptr,
size_t  size,
size_t  count,
FILE stream 
)

Read block of data from stream.

Reads an array of count elements, each one with a size of size bytes, from the stream and stores them in the block of memory specified by ptr. The position indicator of the stream is advanced by the total amount of bytes read.

Parameters
ptrPointer to a block of memory with a size of at least (size*count) bytes, converted to a void*.
sizeSize, in bytes, of each element to be read.
countNumber of elements, each one with a size of size bytes.
streamPointer to a FILE object that identifies the stream to be closed.
Returns
The total number of elements successfully read is returned. If this number differs from the count parameter, either a reading error occurred or the end-of-file was reached while reading. In both cases, the proper indicator is set.

◆ freopen()

FILE* freopen ( const char *  filename,
const char *  mode,
FILE stream 
)

Reopen stream with different file or mode.

Reuses stream to either open the file specified by filename or to change its access mode. If filename is a null pointer, the function attempts to change the mode of the stream.

Parameters
filenameC string containing the name of the file to be opened.
modeC string containing a file access mode (r, w, a, r+, w+, a+).
streamPointer to a FILE object that identifies the stream to be reopened.
Returns
If the file is successfully reopened, the function returns the pointer passed as parameter stream, which can be used to identify the reopened stream. Otherwise, a null pointer is returned.

◆ fseek()

int fseek ( FILE stream,
long int  offset,
int  origin 
)

Reposition stream position indicator.

Sets the position indicator associated with the stream to a new position.

Parameters
streamPointer to a FILE object that identifies the stream to be closed.
offsetNumber of bytes to offset from origin.
originPosition used as reference for the offset. It is specified by one of the constants (file begin: SEEK_SET, current position: SEEK_CUR, end of file: SEEK_END).
Returns
If successful, the function returns zero. Otherwise, it returns non-zero value and error indicator is set.

◆ fsetpos()

int fsetpos ( FILE stream,
const fpos_t pos 
)

Set position indicator of stream.

Restores the current position in the stream to pos.

Parameters
streamPointer to a FILE object that identifies the stream to be closed.
posPointer to a fpos_t object containing a position previously obtained with fgetpos.
Returns
If successful, the function returns zero. On failure, a non-zero value is returned and errno is set to a system-specific positive value.

◆ ftell()

long int ftell ( FILE stream)

Get current position in stream.

Returns the current value of the position indicator of the stream.

Parameters
streamPointer to a FILE object that identifies the stream to be closed.
Returns
On success, the current value of the position indicator is returned. On failure, -1 is returned, and errno is set to a system-specific positive value.

◆ fwrite()

size_t fwrite ( const void *  ptr,
size_t  size,
size_t  count,
FILE stream 
)

Write block of data to stream.

Writes an array of count elements, each one with a size of size bytes, from the block of memory pointed by ptr to the current position in the stream. The position indicator of the stream is advanced by the total number of bytes written.

Parameters
ptrPointer to a block of memory with a size of at least (size*count) bytes, converted to a void*.
sizeSize, in bytes, of each element to be read.
countNumber of elements, each one with a size of size bytes.
streamPointer to a FILE object that identifies the stream to be closed.
Returns
The total number of elements successfully written is returned. If this number differs from the count parameter, a writing error prevented the function from completing. In this case, the error indicator will be set for the stream.

◆ getc()

int getc ( FILE stream)

Get character from stream.

Returns the character currently pointed by the internal file position indicator of the specified stream. The internal file position indicator is then advanced to the next character.

Parameters
streamPointer to a FILE object that identifies the stream to be closed.
Returns
On success, the character read is returned. If the position indicator was at the end-of-file, the function returns EOF.

◆ getchar()

int getchar ( )

Get character from stdin.

Returns the next character from the standard input.

Returns
On success, the character read is returned. If the position indicator was at the end-of-file, the function returns EOF.

◆ gets()

char* gets ( char *  str)

Get string from stdin.

Reads characters from the standard input (stdin) and stores them as a C string into str until a newline character or the end-of-file is reached.

Parameters
strPointer to a block of memory (array of char) where the string read is copied as a C string.
Returns
On success, the character read is returned. If the position indicator was at the end-of-file, the function returns EOF.

◆ perror()

void perror ( const char *  str)

Print error message.

◆ printf()

int printf ( const char *  format,
  ... 
)

Print formatted data to stdout.

Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers.

Parameters
formatString that contains text to be written to stdout. Can contain embedded format specifiers.
...Additional arguments
Returns
Total number of characters written to stdout

◆ putc()

int putc ( int  character,
FILE stream 
)

Write character to stream.

Writes a character to the stream and advances the position indicator. The character is written at the position indicated by the internal position indicator of the stream, which is then automatically advanced by one.

Parameters
characterThe int promotion of the character to be written.
streamPointer to a FILE object that identifies the stream to be closed.
Returns
On success, the character written is returned. If a writing error occurs, EOF is returned and the error indicator is set.

◆ putchar()

int putchar ( int  character)

Write character to stdout.

Writes a character to the standard output (stdout).

Parameters
characterThe int promotion of the character to be written.
Returns
On success, the character written is returned. If a writing error occurs, EOF is returned and the error indicator is set.

◆ puts()

int puts ( const char *  str)

Write string to stdout.

Writes the C string pointed by str to the standard output (stdout) and appends a newline character ('
'). The function begins copying from the address specified (str) until it reaches the terminating null character ('\0'). This terminating null-character is not copied to the stream.

Parameters
strC string to be printed.
Returns
On success, the character written is returned. If a writing error occurs, EOF is returned and the error indicator is set.

◆ remove()

int remove ( const char *  filename)

Remove file.

◆ rename()

int rename ( const char *  oldname,
const char *  newname 
)

Rename file.

◆ rewind()

void rewind ( FILE stream)

Set position of stream to the beginning.

Sets the position indicator associated with stream to the beginning of the file.

Parameters
streamPointer to a FILE object that identifies the stream to be closed.

◆ scanf()

int scanf ( const char *  format,
  ... 
)

Read formatted data from stdin.

Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments.

Parameters
formatC string that contains the text to be written to the stream.Can contain embedded format specifiers.
...Depending on the format string, the function may expect a sequence of additional arguments, each containing a pointer to allocated storage where the interpretation of the extracted characters is stored with the appropriate type.
Returns
On success, the function returns the number of items of the argument list successfully filled. Otherwise, EOF.

◆ setbuf()

void setbuf ( FILE stream,
char *  buffer 
)

Set stream buffer.

Specifies the buffer to be used by the stream for I/O operations, which becomes a fully buffered stream. Or, alternatively, if buffer is a null pointer, buffering is disabled for the stream, which becomes an unbuffered stream.

Parameters
streamPointer to a FILE object that identifies the stream to be closed.
bufferUser allocated buffer. Shall be at least BUFSIZ bytes long.

◆ setvbuf()

int setvbuf ( FILE stream,
char *  buffer,
int  mode,
size_t  size 
)

Change stream buffering.

Specifies a buffer for stream. The function allows to specify the mode and size of the buffer (in bytes). If buffer is a null pointer, the function automatically allocates a buffer (using size as a hint on the size to use).

Parameters
streamPointer to a FILE object that identifies the stream to be closed.
bufferUser allocated buffer. Shall be at least BUFSIZ bytes long.
modeSpecifies a mode for file buffering (full buffering: _IOFBF, line buffering: _IOLBF, no buffering: _IONBF).
sizeUser allocated buffer. Shall be at least BUFSIZ bytes long.
Returns
If the buffer is correctly assigned to the file, a zero value is returned. Otherwise, a non-zero value is returned.

◆ sprintf()

int sprintf ( char *  str,
const char *  format,
  ... 
)

Write formatted data to string.

Composes a string with the same text that would be printed if format was used on printf, but instead of being printed, the content is stored as a C string in the buffer pointed by str.

Parameters
strPointer to a buffer where the resulting C-string is stored. The buffer should be large enough to contain the resulting string.
formatString that contains text to be written to buffer. Can contain embedded format specifiers.
...Additional arguments
Returns
Total number of characters written to buffer

◆ ungetc()

int ungetc ( int  character,
FILE stream 
)

Unget character from stream.

A character is virtually put back into an input stream, decreasing its internal file position as if a previous getc operation was undone.

Parameters
characterThe int promotion of the character to be written.
streamPointer to a FILE object that identifies the stream to be closed.
Returns
On success, the character put back is returned. If the operation fails, EOF is returned.

◆ vfprintf()

int vfprintf ( FILE stream,
const char *  format,
va_list  arg 
)

Print formatted data from variable argument list to stream.

Writes the C string pointed by format to the stream. If format includes format specifiers (subsequences beginning with %), the additional arguments stored in arg list are formatted and inserted in the resulting string replacing their respective specifiers.

Parameters
streamPointer to a FILE object that identifies an output stream.
formatC string that contains the text to be written to the stream.Can contain embedded format specifiers.
argA value identifying a variable arguments list initialized with va_start.
Returns
Total number of characters written to stream.

◆ vfscanf()

int vfscanf ( FILE stream,
const char *  format,
va_list  arg 
)

Read formatted data from stream into variable argument list.

Reads data from the stream and stores them according to parameter format into the locations pointed by the elements in the variable argument list identified by arg.

Parameters
streamPointer to a FILE object that identifies an input stream.
formatC string that contains the text to be written to the stream.Can contain embedded format specifiers.
argA value identifying a variable arguments list initialized with va_start.
Returns
On success, the function returns the number of items of the argument list successfully filled. Otherwise, EOF.

◆ vprintf()

int vprintf ( const char *  format,
va_list  arg 
)

Print formatted data from variable argument list to stdout.

Writes the C string pointed by format to the stream. If format includes format specifiers (subsequences beginning with %), the additional arguments stored in arg list are formatted and inserted in the resulting string replacing their respective specifiers.

Parameters
formatC string that contains the text to be written to the stream.Can contain embedded format specifiers.
argA value identifying a variable arguments list initialized with va_start.
Returns
Total number of characters written to stream.

◆ vsprintf()

int vsprintf ( char *  str,
const char *  format,
va_list  arg 
)

Print formatted data from variable argument list to string.

Composes a string with the same text that would be printed if format was used on printf, but using the elements in the variable argument list identified by arg instead of additional function arguments and storing the resulting content as a C string in the buffer pointed by str.

Parameters
strPointer to a buffer where the resulting C-string is stored. The buffer should be large enough to contain the resulting string.
formatC string that contains the text to be written to the stream.Can contain embedded format specifiers.
argA value identifying a variable arguments list initialized with va_start.
Returns
Total number of characters written to stream.

Variable Documentation

◆ stderr

FILE* stderr

Standard error output (default is console).

◆ stdin

FILE* stdin

Standard input (default is keyboard).

◆ stdout

FILE* stdout

Standard output (default is console).