c - 无法正确使用fwrite

标签 c cs50

我无法弄清楚如何在这里使用 fwrite。我遇到了段错误。有没有其他方法可以将 0x00 写入 rgbtRed、rgbtBlue 和 rgbtGreen?我在网上查了一下,但找不到正确的方法。

#include <stdio.h>
#include <stdint.h>
#include <string.h>

typedef uint8_t  BYTE;

typedef struct
{
    BYTE rgbtBlue;
    BYTE rgbtGreen;
    BYTE rgbtRed;
} __attribute__((__packed__))
RGBTRIPLE;

int main (void)
{

    RGBTRIPLE triple;
    /*code which opens a bmp file, copies it and exports it to another 
    file based on the changes which I ask it.
    I'm trying to set R's (of RGB) value to 0 to see if that solves  
    the problem. I'm trying to figure out how to set the value to 0*/

    fwrite(&triple.rgbtRed, sizeof(uint8_t), 1, 0x00);


}

最佳答案

fwrite 用于写入文件,但您希望将内存设置为零,这并不完全相同。

通过这行代码,整个三重结构被设置为零。

memset(&triple, 0, sizeof triple);

关于c - 无法正确使用fwrite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50679782/

相关文章:

C - crypt() - 代码执行 5 个循环或更多比使用 4 个循环但使用相同的参数/哈希需要更长的时间

c - Greedy.c 输出错误的数字,我不知道为什么? (CS50 代码)

c - C 中的守护进程 - 有没有一种方法可以实现它们?

c - 这个字符串怎么能打印出来

将文件扩展名连接到 C 中的基本名称

c - 如何使用快速排序对一对整数的结构进行排序?

c - 我的 Int* 数组是否全部初始化为 NULL?

c - 为什么这个 printf 语句或缺少该语句会改变 for 循环的效果?

c - 我的 cs50 vigenere 代码有什么问题?我很接近输出

c - 如何获取 'isalpha' 遍历字符串的每个字符?