首页 文章

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

提问于
浏览
1

我有一个自定义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 回答

  • 1

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

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

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

相关问题