使用fwrite的功能我不能understasnd finction如何FWRITE在CI工作做了交流program.here其它: -在哪里以及如何在C
#include <stdio.h>
int main()
{
struct books
{
char name[100];
float price;
int pages;
}book1;
puts("enter the name of the book");gets(book1.name);
puts("enter the price of the book");scanf("%f",&book1.price);
puts("enter the pages of the book");scanf("%d",&book1.pages);
FILE *b=fopen("Book.txt","w");
fwrite(&book1,sizeof(book1),1,b);
}
这里是个节目的输入: -
$ ./a.out
enter the name of the book
Voldemort the last BSD user
enter the price of the book
40000
enter the pages of the book
34
,这里是该文件的内容: -
$ cat Book.txt
Voldemort the Great Linux and bsd user!����c����@�@��,B
因此,什么是错在我的计划? fwrite如何工作?我应该什么时候使用它?
你的程序出了什么问题取决于你在第一时间预期的输出。 –
您需要声明变量类型book1,同时忘记关闭文件 –
, c @ @ ,B部分(16字节)实际上是您的价格(8字节)和页面数据(8字节)。您首先需要了解C/C++如何管理数据,然后才能将文件保存到文件中 –