Another common operation is getting or setting the file position. The fs_fgetpos()
and fs_fsetpos()
allow the application to ‘store’ a file location, continue reading or writing the file, and then go back to that place at a later time. An example of using file position get and set is given in Listing - Example file position set and get
Code Block |
---|
language | cpp |
---|
title | Listing - Example file position set and get |
---|
linenumbers | true |
---|
|
void App_Fnct (void)
{
FS_FILE *p_file;
fs_fpos_t pos;
int err;
.
.
.
p_file = fs_fopen("\file.txt", "r"); /* Open file ... */
if (p_file == (FS_FILE *)0) {
APP_TRACE_INFO(("Could not open file."));
return;
}
.
. /* ... read from file. */
.
err = fs_fgetpos(p_file, &pos); /* Save file position ... */
if (err != 0) {
APP_TRACE_INFO(("Could not get file position."));
return;
}
.
. /* ... read some more from file. */
.
err = fs_fsetpos(p_file, &pos); /* Set file to saved position ... */
if (err != 0) {
APP_TRACE_INFO(("Could not set file position."));
return;
}
.
. /* ... read some more from file. */
.
FS_fclose(p_file); /* When finished, close file. */
.
.
.
} |