1
votes

I have a custom File system implementation for SD card. See p. no 14-16. The file can be read or written by a microcontroller connected through a SPI interface with the SD card.

I am storing a BMP image in this file system. But the application requires lots of random jumps from one location to another.

I learnt through this answer that, I basically need fseek(). Unfortunately fseek() is not provided in this system. The functions which are available are only:

fcreate, fopen, fread, fwrite, feof, fclose:

Is it possible to implement fseek() or a similar functionality using the available functions? If yes how? If not, is writing low level (reading sectors , etc) code the only solution?

fcreate

Description: Creates a new file with the specified name.
Prototype: static BYTE fcreate (find_info* findinfo, char* filename)
Parameters: 
1. findinfo-Pointer to a structure with details about the file.
2. filename-Pointer to a memory location that contains the filename.
Return Value: 1, if the file was created successfully. 0, otherwise.

fopen

    Description: Opens a file in one of three modes - Read (r), Write (w), Append (a). 
    In Write/Append modes, if the specified filename does not exist, it is created. 
    In   Write mode, if the specified file exists, it is overwritten.
    Prototype: int fopen (FILE* f, char* filename, char* mode)
    Parameters: 
    1. f-Pointer to file structure 
    2. filename-Pointer to a memory location that contains the filename.
    3. mode-Pointer to a memory location that contains the file open mode.
    Return Value: 1, if file was opened successfully. 0, otherwise.

fread

Description: Reads the specified number of bytes from a file.
Prototype: unsigned fread (FILE* f, BYTE* buffer, unsigned count)
Parameters: 
1. f-Pointer to file structure
2. buffer-Pointer to a memory locationwhere the data will be copied to.
3. count-The maximum number of bytes to read from the file.
Return Value: Number of bytes read from the file.

fwrite

Description: Writes the specified number of bytes to a file.
Prototype: unsigned fwrite (FILE* f, BYTE* buffer, unsigned count)
Parameters: 
1. f-Pointer to file structure
2. buffer-Pointer to a memory location where the data will be copied from.
3. count-The number of bytes to write to the file.
Return Value: Number of bytes written to the file.

feof

Description: Checks whether the specified file's current position pointer has reached the end of the file.
Prototype: int feof (FILE* f)
Parameters: 
1. f-Pointer to file structure
Return Value: 0, if the file'scurrent position pointer has not reached the end of the file.    1, if the file's current position pointer has reached the end of the file.

fclose

Description: Closes a file.
Prototype: void fclose (FILE* f)
Parameters: 1. f-Pointer to file structure
Return Value: None.
1
Due to the fact that fseek is a library function just look at the fseek implementation in several libraries out there.akluth

1 Answers

1
votes

Short answer: it's very unlikely if the file system code doesn't support it itself.

Long answer: fseek() in larger operating systems is usually implemented with a lseek() system call, which executes kernel code which modifies the data associated with the file descriptor. Because you have an embedded environment you may not have an equivalent of lseek() (I don' know...).

BUT, in your example and in the documentation, you are interfacing with the FILE structure, which usually means there's C code, very likely accessible to you, which implements the FILE interface. The FILE data type is usually a structure declared in stdio.h, which typically has pointers to various functions doing the actual read, write and seek operations. Your best chance is to find your stdio.hr, see how FILE is declared and if it supports the seek operation, then if it does, see how the function pointers are filled by the fopen() operation (you need to see the C implementation of fopen()).