使用其他文件系统函数实现fseek()功能

时间:2013-10-11 14:18:37

标签: c file-io

我有自定义File system implementation for SD card。见第没有14-16。该文件可由通过SPI接口与SD卡连接的微控制器读取或写入。

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

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

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

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

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 个答案:

答案 0 :(得分:1)

简短回答:如果文件系统代码本身不支持它,则不太可能。

长答案:较大操作系统中的fseek()通常使用lseek()系统调用实现,该调用执行内核代码,该内核代码修改与文件描述符关联的数据。因为你有一个嵌入式环境,你可能没有相当于lseek()(我不知道......)。

但是,在您的示例和文档中,您正在与FILE结构连接,这通常意味着您很可能可以访问C代码,它实现了FILE接口。 FILE数据类型通常是在stdio.h中声明的结构,它通常具有指向执行实际读取,写入和查找操作的各种函数的指针。你最好的机会是找到你的stdio.hr,看看如何声明FILE以及它是否支持搜索操作,如果是,请查看fopen()操作如何填充函数指针(需要查看C实现) of fopen())。