m2mb API docs  25.30.003
m2mb API sets documentation
m2mb_fs_stdio.h File Reference

File system stdio library implementation. More...

Go to the source code of this file.

Macros

#define NULL   0
 
#define M2MB_SEEK_SET   0 /* Seek from beginning of file. */
 
#define M2MB_SEEK_CUR   1 /* Seek from current position. */
 
#define M2MB_SEEK_END   2 /* Seek from end of file. */
 

Typedefs

typedef struct M2MB_FILE_TAG M2MB_FILE_T
 

Functions

M2MB_FILE_T * m2mb_fs_fopen (const CHAR *path, const CHAR *mode)
 Open file as stream. More...
 
SIZE_T m2mb_fs_fread (void *ptr, SIZE_T size, SIZE_T nitems, M2MB_FILE_T *stream)
 Read file as stream. More...
 
SIZE_T m2mb_fs_fwrite (void *ptr, SIZE_T size, SIZE_T nitems, M2MB_FILE_T *stream)
 Write file as stream. More...
 
INT32 m2mb_fs_fclose (M2MB_FILE_T *stream)
 Close file as stream. More...
 
INT32 m2mb_fs_fseek (M2MB_FILE_T *stream, INT32 offset, INT32 whence)
 Move file position indicator. More...
 
INT32 m2mb_fs_ftell (M2MB_FILE_T *stream)
 Get file position indicator. More...
 
INT32 m2mb_fs_fflush (M2MB_FILE_T *stream)
 Flush file as stream. More...
 
INT32 m2mb_fs_fileno (M2MB_FILE_T *stream)
 Get file integer descriptor. More...
 
INT32 m2mb_fs_fgetc (M2MB_FILE_T *stream)
 Read char from file as stream. More...
 
CHAR * m2mb_fs_fgets (CHAR *s, INT32 size, M2MB_FILE_T *stream)
 Read line string from file as stream. More...
 
INT32 m2mb_fs_fputc (INT32 c, M2MB_FILE_T *stream)
 Write char to file as stream. More...
 
INT32 m2mb_fs_fputs (const CHAR *s, M2MB_FILE_T *stream)
 Write string to file as stream. More...
 
INT32 m2mb_fs_remove (const CHAR *path)
 Delete file. More...
 
INT32 m2mb_fs_rename (const CHAR *oldpath, const CHAR *newpath)
 Rename file or directory. More...
 

Detailed Description

File system stdio library implementation.

m2m/m2m_common/m2mb_inc/m2mb_fs_stdio.h

stdio library for files. The following functions are implemented: m2mb_fs_fopen m2mb_fs_fread m2mb_fs_fwrite m2mb_fs_fclose m2mb_fs_fseek m2mb_fs_ftell m2mb_fs_fflush m2mb_fs_fileno m2mb_fs_fgetc m2mb_fs_fgets m2mb_fs_fputc m2mb_fs_fputs m2mb_fs_remove m2mb_fs_rename

@notes Dependencies: m2m/m2m_common/m2mb_inc/m2mb_types.h

Author
Giorgio De Pauli
Date
20/02/2017

Function Documentation

◆ m2mb_fs_fclose()

INT32 m2mb_fs_fclose ( M2MB_FILE_T *  stream)

Close file as stream.

The function flushes the stream pointed to by stream (writing any buffered output data) and closes the underlying file descriptor.

Parameters
[in]streamPointer to stream file object.
Returns
Upon successful completion 0 is returned. Otherwise, -1 is returned.
Note
<Notes>

Example

<C code example>

◆ m2mb_fs_fflush()

INT32 m2mb_fs_fflush ( M2MB_FILE_T *  stream)

Flush file as stream.

The function forces a write of all user-space buffered data for the given output.

Parameters
[in]streamPointer to stream file object.
Returns
Upon successful completion 0 is returned. Otherwise, EOF is returned.
Note
<Notes>

Example

<C code example>

◆ m2mb_fs_fgetc()

INT32 m2mb_fs_fgetc ( M2MB_FILE_T *  stream)

Read char from file as stream.

The function reads the next character from the stream pointed to by stream.

Parameters
[in]streamPointer to stream file object.
Returns
On success returns the character read as an unsigned char cast to an int. If an error occurs, or the end of the file is reached, -1 is returned.
Note
<Notes>

Example

<C code example>

◆ m2mb_fs_fgets()

CHAR* m2mb_fs_fgets ( CHAR *  s,
INT32  size,
M2MB_FILE_T *  stream 
)

Read line string from file as stream.

The function reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an end of file or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.

Parameters
[out]sBuffer to fill with read data.
[in]sizeSize of buffer s. One more than the max number of characters to be read.
[in]streamPointer to stream file object.
Returns
On success returns s (the characters read as terminated string). If an error occurs, or the end of the file is reached and no characters have been read, NULL is returned.
Note
<Notes>

Example

<C code example>

◆ m2mb_fs_fileno()

INT32 m2mb_fs_fileno ( M2MB_FILE_T *  stream)

Get file integer descriptor.

The function examines the argument stream and returns its integer descriptor.

Parameters
[in]streamPointer to stream file object.
Returns
Upon successful completion the file integer descriptor is returned. Otherwise, -1 is returned.
Note
<Notes>

Example

<C code example>

◆ m2mb_fs_fopen()

M2MB_FILE_T* m2mb_fs_fopen ( const CHAR *  path,
const CHAR *  mode 
)

Open file as stream.

The function opens the file whose name is the string pointed to by path and associates a stream with it.

Parameters
[in]pathName of file to be open.
[in]modeMode for opening file. r Open text file for reading. The stream is positioned at the beginning of the file. r+ Open for reading and writing. The stream is positioned at the beginning of the file. w Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file. w+ Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file. a Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file. a+ Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file. The mode string can also include the letter 'b' either as a last character or as a character between the characters in any of the two-character strings described above. The 'b' is ignored.
Returns
Upon successful completion returns a pointer to stream file object. Otherwise, NULL is returned.
Note
<Notes>

Example

<C code example>

◆ m2mb_fs_fputc()

INT32 m2mb_fs_fputc ( INT32  c,
M2MB_FILE_T *  stream 
)

Write char to file as stream.

The function writes the character c, cast to an unsigned char, to the stream pointed to by stream.

Parameters
[in]cCharacter to be written.
[in]streamPointer to stream file object.
Returns
On success returns the character written as an unsigned char cast to an int. If an error occurs, -1 is returned.
Note
<Notes>

Example

<C code example>

◆ m2mb_fs_fputs()

INT32 m2mb_fs_fputs ( const CHAR *  s,
M2MB_FILE_T *  stream 
)

Write string to file as stream.

The function writes the string s to stream, without its terminating null byte ('\0').

Parameters
[in]sNull byte terminated string to be written.
[in]streamPointer to stream file object.
Returns
On success returns a nonnegative number. If an error occurs, -1 is returned.
Note
<Notes>

Example

<C code example>

◆ m2mb_fs_fread()

SIZE_T m2mb_fs_fread ( void *  ptr,
SIZE_T  size,
SIZE_T  nitems,
M2MB_FILE_T *  stream 
)

Read file as stream.

The function reads nitems elements of data, each size bytes long, from the stream pointed to by stream, storing them at the location given by ptr.

Parameters
[out]ptrBuffer to fill with read data.
[in]sizeSize in bytes of each item.
[in]nitemsNumber of items to read.
[in]streamPointer to stream file object.
Returns
On success returns the number of items read. If an error occurs, or the end of the file is reached, the return value is a short item count (or zero).
Note
<Notes>

Example

<C code example>

◆ m2mb_fs_fseek()

INT32 m2mb_fs_fseek ( M2MB_FILE_T *  stream,
INT32  offset,
INT32  whence 
)

Move file position indicator.

The function sets the file position indicator for the stream pointed to by stream. The new position, measured in bytes, is obtained by adding offset bytes to the position specified by whence.

Parameters
[in]streamPointer to stream file object.
[in]offsetNumber of bytes to move the file position indicator from the position specified by whence.
[in]whenceFile position. M2MB_SEEK_SET Beginning of the file. M2MB_SEEK_CUR Current file position. M2MB_SEEK_END End of file.
Returns
Upon successful completion returns 0. Otherwise, -1 is returned.
Note
<Notes>

Example

<C code example>

◆ m2mb_fs_ftell()

INT32 m2mb_fs_ftell ( M2MB_FILE_T *  stream)

Get file position indicator.

The function obtains the current value of the file position indicator for the stream pointed to by stream.

Parameters
[in]streamPointer to stream file object.
Returns
Upon successful completion returns the current offset. Otherwise, -1 is returned.
Note
<Notes>

Example

<C code example>

◆ m2mb_fs_fwrite()

SIZE_T m2mb_fs_fwrite ( void *  ptr,
SIZE_T  size,
SIZE_T  nitems,
M2MB_FILE_T *  stream 
)

Write file as stream.

The function writes nitems elements of data, each size bytes long, to the stream pointed to by stream, obtaining them from the location given by ptr.

Parameters
[in]ptrBuffer containing data to be written.
[in]sizeSize in bytes of each item.
[in]nitemsNumber of items to write.
[in]streamPointer to stream file object.
Returns
On success returns the number of items written. If an error occurs, or the end of the file is reached, the return value is a short item count (or zero).
Note
<Notes>

Example

<C code example>

◆ m2mb_fs_remove()

INT32 m2mb_fs_remove ( const CHAR *  path)

Delete file.

The function deletes the file whose name is the string pointed to by path.

Parameters
[in]pathName of file to be deleted.
Returns
Upon successful completion returns 0. Otherwise, -1 is returned.
Note
<Notes>

Example

<C code example>

◆ m2mb_fs_rename()

INT32 m2mb_fs_rename ( const CHAR *  oldpath,
const CHAR *  newpath 
)

Rename file or directory.

The function changes the name of a file or a directory. The old argument points to the pathname of the file to be renamed. The new argument points to the new pathname of the file.

Parameters
[in]oldpathName of file to be renamed.
[in]newpathName of new file.
Returns
Upon successful completion returns 0. Otherwise, -1 is returned.
Note
<Notes>

Example

<C code example>