gpt4 book ai didi

c - 为什么 fwrite 不适用于 C 中的二进制文件?

转载 作者:太空宇宙 更新时间:2023-11-04 07:13:07 25 4
gpt4 key购买 nike

我想用 C 语言将一些数据写入二进制文件。显然,函数 fwrite 似乎不能正常工作。这是代码:

typedef struct tw {
char name[40];
char tweet[150];
int rt, like;
} Twitter;

void createFile() {
FILE *bin;
Twitter user;
if (bin = fopen("test.bin", "wb") == NULL) {
printf("Error");
return 0;
}
strcpy(user.name, "name");
strcpy(user.tweet, "this is a tweet");
user.rt=5;
user.like=10;

fwrite(&user, sizeof(Twitter), 1, bin);

fclose(bin);

}

fwrite 函数不会向文件中写入任何内容,我已经在代码中查找错误,但找不到任何错误。此外,当我尝试使用该函数写入 txt 文件时,它工作正常。

最佳答案

你需要改变

if (bin = fopen("test.bin", "wb") == NULL)

 if ((bin = fopen("test.bin", "wb")) == NULL)

关于c - 为什么 fwrite 不适用于 C 中的二进制文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26821139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com