[英]Why fwrite doesn't work for binary files in C?
I want to write some data into a binary file, using C language. 我想使用C语言将一些数据写入二进制文件。 Apparently, the function fwrite doesn't seem to work properly.
显然,函数fwrite似乎无法正常工作。 This is the code:
这是代码:
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);
}
The fwrite function doesn't write anything into the file, and I've looked for mistakes in the code, but I couldn't find any. fwrite函数不会向文件中写入任何内容,并且我一直在寻找代码中的错误,但找不到任何错误。 Also, when I tried to use the function to write into a txt file, it worked correctly.
另外,当我尝试使用该功能写入txt文件时,它可以正常工作。
You need to change 你需要改变
if (bin = fopen("test.bin", "wb") == NULL)
to 至
if ((bin = fopen("test.bin", "wb")) == NULL)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.