WE310F5  39.00.000
M2MB FS STDIO

This section describes the M2MB APIs to perform various File system stdio operations. More...

#define NULL   0
 
#define M2MB_SEEK_SET   0
 
#define M2MB_SEEK_CUR   1
 
#define M2MB_SEEK_END   2
 
typedef struct M2MB_FILE_TAG M2MB_FILE_T
 
M2MB_FILE_Tm2mb_fs_fopen (const CHAR *path, const CHAR *mode)
 Opens 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_fgetc (M2MB_FILE_T *stream)
 Read char from file as stream. More...
 
CHARm2mb_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

This section describes the M2MB APIs to perform various File system stdio operations.

Macro Definition Documentation

◆ M2MB_SEEK_CUR

#define M2MB_SEEK_CUR   1

Seek from current position.

Definition at line 53 of file m2mb_fs_stdio.h.

◆ M2MB_SEEK_END

#define M2MB_SEEK_END   2

Seek from end of file.

Definition at line 54 of file m2mb_fs_stdio.h.

◆ M2MB_SEEK_SET

#define M2MB_SEEK_SET   0

Seek from beginning of file.

Definition at line 52 of file m2mb_fs_stdio.h.

◆ NULL

#define NULL   0

Definition at line 47 of file m2mb_fs_stdio.h.

Typedef Documentation

◆ M2MB_FILE_T

typedef struct M2MB_FILE_TAG M2MB_FILE_T

Definition at line 59 of file m2mb_fs_stdio.h.

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, returns 0. Otherwise, -1 is returned.
Note
<Notes>

m2mb_fs_fclose(file_fd);

#define M2MB_PROV_FILE_CUST_JS_PATH "/spinor/web/js/custom.min.js.gz"
struct Student
{
int roll;
char name[25];
float marks;
};
int main()
{
M2MB_FILE_T * file_fd;
char ch;
struct Student Stu;
file_fd = m2mb_fs_fopen( M2MB_PROV_FILE_CUST_JS_PATH, r+ );
if(file_fd == NULL)
{
return M2MB_ERROR;
}
while(m2mb_fs_fread(&Stu,sizeof(Stu),1,file_fd)>0)
printf("\n\t%d\t%s\t%f",Stu.roll,Stu.name,Stu.marks);
m2mb_fs_fclose(file_fd);
return M2MB_OK;
}

◆ 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>

m2mb_fs_fgetc(file_fd);

#define M2MB_PROV_FILE_CUST_JS_PATH "/spinor/web/js/custom.min.js.gz"
int main()
{
M2MB_FILE_T * file_fd;
file_fd = m2mb_fs_fopen( M2MB_PROV_FILE_CUST_JS_PATH, r+ );
if(file_fd == NULL)
{
return M2MB_ERROR;
}
// Taking input single character at a time
char c = m2mb_fs_fgetc(file_fd);
printf("%c", c);
return M2MB_OK;
}

◆ 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>

m2mb_fs_fgets(str,80,fp);

#define M2MB_PROV_FILE_CUST_JS_PATH "/spinor/web/js/custom.min.js.gz"
int main()
{
M2MB_FILE_T * file_fd;
char str[80];
file_fd = m2mb_fs_fopen( M2MB_PROV_FILE_CUST_JS_PATH, r+ );
if(file_fd == NULL)
{
return M2MB_ERROR;
}
while((m2mb_fs_fgets(str,80,file_fd))!=NULL)
printf("%s",str);
return M2MB_OK;
}

◆ m2mb_fs_fopen()

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

Opens 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>

m2mb_fs_fopen( M2MB_PROV_FILE_CUST_JS_PATH, r+ );

#define M2MB_PROV_FILE_CUST_JS_PATH "/spinor/web/js/custom.min.js.gz"
int main()
{
M2MB_FILE_T * file_fd;
file_fd = m2mb_fs_fopen( M2MB_PROV_FILE_CUST_JS_PATH, r );
if(file_fd == NULL)
{
return M2MB_ERROR;
}
return M2MB_OK;
}

◆ 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>

m2mb_fs_fputc(string[i], file_fd);

#define M2MB_PROV_FILE_CUST_JS_PATH "/spinor/web/js/custom.min.js.gz"
int main()
{
M2MB_FILE_T * file_fd;
file_fd = m2mb_fs_fopen( M2MB_PROV_FILE_CUST_JS_PATH, r+ );
if(file_fd == NULL)
{
return M2MB_ERROR;
}
char string[] = "good bye";
for (int i = 0; string[i]!='\0'; i++)
{
// Input string into the file
// single character at a time
m2mb_fs_fputc(string[i], file_fd);
}
return M2MB_OK;
}

◆ 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>

m2mb_fs_fputs(string,file_fd);

#define M2MB_PROV_FILE_CUST_JS_PATH "/spinor/web/js/custom.min.js.gz"
int main()
{
M2MB_FILE_T * file_fd;
char string[] = "good bye";
file_fd = m2mb_fs_fopen( M2MB_PROV_FILE_CUST_JS_PATH, r+ );
if(file_fd == NULL)
{
return M2MB_ERROR;
}
do
{
m2mb_fs_fputs(string,file_fd);
}while(strlen(string)!=0);
return M2MB_OK;
}

◆ 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>

m2mb_fs_fread(&Stu,sizeof(Stu),1,file_fd)

#define M2MB_PROV_FILE_CUST_JS_PATH "/spinor/web/js/custom.min.js.gz"
struct Student
{
int roll;
char name[25];
float marks;
};
int main()
{
M2MB_FILE_T * file_fd;
char ch;
struct Student Stu;
file_fd = m2mb_fs_fopen( M2MB_PROV_FILE_CUST_JS_PATH, r+ );
if(file_fd == NULL)
{
return M2MB_ERROR;
}
while(m2mb_fs_fread(&Stu,sizeof(Stu),1,file_fd)>0)
printf("\n\t%d\t%s\t%f",Stu.roll,Stu.name,Stu.marks);
return M2MB_OK;
}

◆ 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>

m2mb_fs_fseek(file_fd, 0, M2MB_SEEK_END);

#define M2MB_PROV_FILE_CUST_JS_PATH "/spinor/web/js/custom.min.js.gz"
int main()
{
M2MB_FILE_T * file_fd;
file_fd = m2mb_fs_fopen( M2MB_PROV_FILE_CUST_JS_PATH, r+ );
if(file_fd == NULL)
{
return M2MB_ERROR;
}
// Moving pointer to end
// Printing position of pointer
printf("%ld", m2mb_fs_ftell(file_fd));
return M2MB_OK;
}

◆ 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>

m2mb_fs_ftell(file_fd);

#define M2MB_PROV_FILE_CUST_JS_PATH "/spinor/web/js/custom.min.js.gz"
int main()
{
M2MB_FILE_T * file_fd;
file_fd = m2mb_fs_fopen( M2MB_PROV_FILE_CUST_JS_PATH, r+ );
if(file_fd == NULL)
{
return M2MB_ERROR;
}
// Moving pointer to end
// Printing position of pointer
printf("%ld", m2mb_fs_ftell(file_fd));
return M2MB_OK;
}

◆ 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>

m2mb_fs_fwrite(&Stu,sizeof(Stu),1,file_fd);

#define M2MB_PROV_FILE_CUST_JS_PATH "/spinor/web/js/custom.min.js.gz"
struct Student
{
int roll;
char name[25];
float marks;
};
int main()
{
M2MB_FILE_T * file_fd;
char ch;
struct Student Stu;
file_fd = m2mb_fs_fopen( M2MB_PROV_FILE_CUST_JS_PATH, r+ );
if(file_fd == NULL)
{
return M2MB_ERROR;
}
do
{
printf("\nEnter Roll : ");
scanf("%d",&Stu.roll);
printf("Enter Name : ");
scanf("%s",Stu.name);
printf("Enter Marks : ");
scanf("%f",&Stu.marks);
m2mb_fs_fwrite(&Stu,sizeof(Stu),1,file_fd);
printf("\nDo you want to add another data (y/n) : ");
ch = getche();
}while(ch=='y' || ch=='Y');
printf("\nData written successfully...");
return M2MB_OK;
}

◆ 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>

m2mb_fs_remove(M2MB_PROV_FILE_CUST_JS_PATH);

#define M2MB_PROV_FILE_CUST_JS_PATH "/spinor/web/js/custom.min.js.gz"
int main()
{
M2MB_FILE_T * file_fd;
file_fd = m2mb_fs_fopen( M2MB_PROV_FILE_CUST_JS_PATH, r+ );
if(file_fd == NULL)
{
return M2MB_ERROR;
}
// Taking input single character at a time
char c = m2mb_fs_fgetc(file_fd);
printf("%c", c);
m2mb_fs_remove(M2MB_PROV_FILE_CUST_JS_PATH);
return M2MB_OK;
}

◆ 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>

m2mb_fs_rename( M2MB_PROV_FILE_CUST_JS_PATH,M2MB_PROV_FILE_NEW__PATH);

#define M2MB_PROV_FILE_CUST_JS_PATH "/spinor/web/js/custom.min.js.gz"
#define M2MB_PROV_FILE_NEW__PATH "/spinor/web/js/newcustom.min.js.gz"
int main()
{
INT32 ret;
ret = m2mb_fs_rename( M2MB_PROV_FILE_CUST_JS_PATH,M2MB_PROV_FILE_NEW__PATH);
if(ret != M2MB_OK)
{
return M2MB_ERROR;
}
return M2MB_OK;
}