将文件以指定格式存储~BMP~C的实现~FAT32格式

参考:

BMP文件学习笔记(一): C/C++语言实现.bmp文件读写_c++ 写入bmp-CSDN博客

U盘到底用什么格式好?FAT32、NTFS还是exFAT? - 知乎 (zhihu.com)

第一部分:

位图文件头

BITMAPFILEHEADER,是一个结构。这个结构的长度是固定的,为14个字节

        (WORD为无符号16位整数,DWORD为无符号32位整数)。

其定义如下:

typedef struct tagBITMAPFILEHEADER {
WORD           bfType;
//位图文件类型,必须是0x4D42,即字符串“BM”,也就是说所有.bmp文件的头两个字节都是“BM”
DWORD          bfSize;//位图文件大小,包括这14个字节
WORD           bfReserved1;//保留字,设为0
WORD           bfReserved2;//保留字,设为0
DWORD        bfOffBits;//从文件头到实际的位图数据的偏移字节数,单位:字节
} BITMAPFILEHEADER;

WORD           bfType;//4D42 = BM
DWORD          bfSize;//1920x1080x3+16=5E EC 36
WORD           bfReserved1;//保留字,设为0
WORD           bfReserved2;//保留字,设为0
DWORD          bfOffBits;  //从文件头到实际的位图数据的偏移字节数,单位:字节
                           //Head_Length = 54 byte = 36 hex 

第二部分:

位图信息头

BITMAPINFOHEADER,

是一个结构。这个结构的长度也是固定的,为40个字节(LONG为32位整数)。

其定义如下:

typedef struct tagBITMAPINFOHEADER{
DWORD        biSize;//本结构所占用字节数,大小为40字节
LONG         biWidth;//位图宽度,单位:字节
LONG         biHeight;//位图高度,单位:字节
WORD         biPlanes;//目标设备级别,必须为1
WORD         biBitCount;
//表示颜色时每个像素要用到的位数,常用的值为1(黑白二色图), 4(16色图), 8(256色), 24(真彩色图)
DWORD        biCompression;
// 位图是否压缩,其类型是 0(BI_RGB不压缩),  1(BI_RLE8压缩类型)或2(BI_RLE4压缩类型)
DWORD        biSizeImage;//实际的位图数据占用的字节数
LONG         biXPelsPerMeter;//位图水平分辨率,每米像素数
LONG         biYPelsPerMeter;//位图垂直分辨率,每米像素数
DWORD        biClrUsed;
//指定本图象实际用到的颜色数,如果该值为零,则用到的颜色数为2的biBitCount次幂个
DWORD        biClrImportant;//指定本图象中重要的颜色数,如果该值为零,则认为所有的颜色都是重要的
} BITMAPINFOHEADER;

FatFs - Generic FAT Filesystem Module (elm-chan.org)

 f_mount

FRESULT f_mount (
  FATFS*       fs,    /* [IN] Filesystem object */
  const TCHAR* path,  /* [IN] Logical drive number */
  BYTE         opt    /* [IN] Initialization option */
);

参数

fs:文件系统的对象指针

Pointer to the filesystem object to be registered and cleared. Null pointer unregisters the registered filesystem object.

指向要注册和清除的文件系统对象的指针。空指针取消注册已注册的文件系统对象。

path:逻辑分区的标号,一般是0

Pointer to the null-terminated string that specifies the logical drive. The string without drive number means the default drive.

指向指定逻辑驱动器的以 null 结尾的字符串的指针。不带驱动器号的字符串表示默认驱动器。

opt

Mounting option.

0: Do not mount now (to be mounted on the first access to the volume),

1: Force mounted the volume to check if it is ready to work.

0:现在不安装(将在第一次访问卷时挂载),

1:强制挂载以检查它是否准备好工作。

f_open 

FRESULT f_open (
  FIL* fp,           /* [OUT] Pointer to the file object structure */
  const TCHAR* path, /* [IN] File name */
  BYTE mode          /* [IN] Mode flags */
);

fp

Pointer to the blank file object structure.

指向空白文件对象结构的指针。

path

Pointer to the null-terminated string that specifies the file name to open or create.

指向以 null 结尾的字符串的指针,该字符串指定要打开或创建的文件名。

mode

Mode flags that specifies the type of access and open method for the file. It is specified by a combination of following flags.

指定文件的访问类型和打开方法的模式标志。它由以下标志的组合指定。

Flags Meaning
FA_READ Specifies read access to the file. Data can be read from the file.
FA_WRITE Specifies write access to the file. Data can be written to the file. Combine with FA_READ for read-write access.
FA_OPEN_EXISTING Opens a file. The function fails if the file is not existing. (Default)
FA_CREATE_NEW Creates a new file. The function fails with FR_EXIST if the file is existing.
FA_CREATE_ALWAYS Creates a new file. If the file is existing, it will be truncated and overwritten.
FA_OPEN_ALWAYS Opens the file if it is existing. If not, a new file will be created.
FA_OPEN_APPEND Same as FA_OPEN_ALWAYS except the read/write pointer is set end of the file.
POSIX FatFs
"r" FA_READ
"r+" FA_READ | FA_WRITE
"w" FA_CREATE_ALWAYS | FA_WRITE
"w+" FA_CREATE_ALWAYS | FA_WRITE | FA_READ
"a" FA_OPEN_APPEND | FA_WRITE
"a+" FA_OPEN_APPEND | FA_WRITE | FA_READ
"wx" FA_CREATE_NEW | FA_WRITE
"w+x" FA_CREATE_NEW | FA_WRITE | FA_READ

f_lseek:移动指针

FRESULT f_lseek (
  FIL*    fp,  /* [IN] File object */
  FSIZE_t ofs  /* [IN] Offset of file read/write pointer to be set */
);

fp

Pointer to the open file object.

指向打开的文件对象的指针。

ofs

Byte offset from top of the file to set read/write pointer. The data type FSIZE_t is an alias of either DWORD(32-bit) or QWORD(64-bit) depends on the configuration option FF_FS_EXFAT.

ofs 从文件顶部开始的字节偏移量,用于设置读/写指针。

数据类型 FSIZE_t 是 DWORD(32 位)或 QWORD(64 位)的别名,

具体取决于配置选项 FF_FS_EXFAT。

/* Open file */
    fp = malloc(sizeof (FIL));
    res = f_open(fp, "file.dat", FA_READ|FA_WRITE);
    if (res) ...

    /* Set read/write pointer to 5000 */
    res = f_lseek(fp, 5000);

    /* Set read/write pointer to end of the file to append data */
    res = f_lseek(fp, f_size(fp));

    /* Advance read/write pointer 3000 bytes */
    res = f_lseek(fp, f_tell(fp) + 3000);

    /* Rewind read/write pointer 2000 bytes (take care on wraparound) */
    res = f_lseek(fp, f_tell(fp) - 2000);

f_write

FRESULT f_write (
  FIL* fp,          /* [IN] Pointer to the file object structure */
  const void* buff, /* [IN] Pointer to the data to be written */
  UINT btw,         /* [IN] Number of bytes to write */
  UINT* bw          /* [OUT] Pointer to the variable to return number of bytes written */
);

fp

Pointer to the open file object structure.

指向打开文件对象结构的指针。

buff

Pointer to the data to be written.

指向要写入的数据的指针。

btw:希望写入的字节数量

Specifies number of bytes to write in range of UINT type. If the data needs to be written fast, it should be written in large chunk as possible.

指定在 UINT 类型范围内写入的字节数。如果需要快速写入数据,则应尽可能以大块的形式写入。

bw:指向变量的指针,返回写入的字节数

Pointer to the UINT variable that receives the number of bytes written. This value is always valid after the function call regardless of the function return code. If the return value is equal to btw, the function return code should be FR_OK.

指向接收写入字节数的 UINT 变量的指针。无论函数返回码如何,该值在函数调用后始终有效。如果返回值等于btw,则函数返回码应为FR_OK。

f_sync

f_sync 函数刷新写入文件的缓存信息。

FRESULT f_sync (
  FIL* fp     /* [IN] File object */
);

f_close

FRESULT f_close (
  FIL* fp     /* [IN] Pointer to the file object */
);

你可能感兴趣的:(c语言,开发语言)