1

我有一个用于 SD 卡的自定义文件系统实现。见第 没有 14-16。该文件可由通过 SPI 接口与 SD 卡连接的微控制器读取或写入。

我在这个文件系统中存储了一个 BMP 图像。但是该应用程序需要从一个位置到另一个位置的大量随机跳转。

我通过这个答案了解到,我基本上需要 fseek()。不幸的是,这个系统没有提供 fseek()。可用的功能只有:

fcreate、fopen、fread、fwrite、feof、fclose:

是否可以使用可用功能实现 fseek() 或类似功能?如果是怎么办?如果不是,编写低级(读取扇区等)代码是唯一的解决方案吗?

创建

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.

开放

    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.

恐惧

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.

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.

关注

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.

关闭

Description: Closes a file.
Prototype: void fclose (FILE* f)
Parameters: 1. f-Pointer to file structure
Return Value: None.
4

1 回答 1

1

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()).

于 2013-10-11T14:33:13.223 回答