我想用 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)
我是一名优秀的程序员,十分优秀!