有人能告诉我为什么这个函数不起作用吗?我就是搞不懂……
void writeRegister(FILE *arq, Book *L){ //writes in actual file position
char c = '|';
int sizeRegWrite = reglen(L); //reglen() returns the size of Book
fwrite(&sizeRegWrite, sizeof(int), 1, arq);
fwrite(L->TITLE, sizeof(char), strlen(L->TITLE), arq);
fwrite(&c, sizeof(char), 1, arq); //writing delimiter
fwrite(L->AUTHOR, sizeof(char), strlen(L->AUTHOR), arq);
fwrite(&c, sizeof(char), 1, arq); //writing delimiter
fwrite(L->PUBLISHER, sizeof(char), strlen(L->PUBLISHER), arq);
fwrite(&c, sizeof(char), 1, arq); //writing delimiter
fwrite(L->YEAR, sizeof(int), 1, arq);
fwrite(&c, sizeof(char), 1, arq); //writing delimiter
fwrite(L->LANGUAGE, sizeof(char), strlen(L->LANGUAGE), arq);
fwrite(&c, sizeof(char), 1, arq); //writing delimiter
fwrite(L->PAGES, sizeof(int), 1, arq);
fwrite(&c, sizeof(char), 1, arq); //writing delimiter
fwrite(L->PRICE, sizeof(float), 1, arq);
fwrite(&c, sizeof(char), 1, arq); //writing delimiter
return;
}
结构体Book的声明如下:
typedef struct {
char *TITLE;
char *AUTHOR;
char *PUBLISHER;
int YEAR;
char *LANGUAGE;
int PAGES;
float PRICE;
} Book;
主要int main() {
FILE *arq = fopen("BD_books2.bin", "rb+");
if(arq == NULL)
printf("Error while opening file!!!");
Book L;
readData(&L); //Reads all fields from keyboard and places in Book. Working properly
writeRegister(arq, &L);
system("pause");
return 0;
}
必须在结构体内部使用这些指针,所以不能删除它们。顺便说一下,我的问题只是那些integers
和float
。
这个函数只有在我用fprintf()
写所有的整数和浮点数(YEAR, PAGES和PRICE)时才能正常工作,但是我把它写在二进制文件中,当然我想用二进制来写,所以我试图使用fwrite()。
另一件事是:编译器将incompatible type for argument 1 of 'fwrite'
指向这一行:fwrite(L->PRICE, sizeof(float), 1, arq);
fwrite的第一个参数需要一个指针
像下面这样的行:
fwrite(L->PAGES, sizeof(int), 1, arq);
应该这样写:
fwrite(&(L->PAGES), sizeof(int), 1, arq);
对于该结构体的YEAR和PRICE成员也是如此
fwrite(&(L->YEAR), sizeof(int), 1, arq);
...
fwrite(&(L->PRICE), sizeof(float), 1, arq);
注意,您不需要对TITLE、PUBLISHER和AUTHOR进行相同的更改,因为这些成员字段的类型已经是指针(char*)。
fwrite
的签名为
std::size_t fwrite( const void* buffer, std::size_t size, std::size_t count, std::FILE* stream );
函数的第一个实参必须是指针。我很惊讶你没有得到编译错误的以下行。
fwrite(L->YEAR, sizeof(int), 1, arq);
fwrite(L->PAGES, sizeof(int), 1, arq);
fwrite(L->PRICE, sizeof(float), 1, arq);
它们需要
fwrite(&(L->YEAR), sizeof(int), 1, arq);
fwrite(&(L->PAGES), sizeof(int), 1, arq);
fwrite(&(L->PRICE), sizeof(float), 1, arq);
此外,检查所有IO函数的返回值以确保它们按您的期望工作是一个很好的做法。
if ( fwrite(&(L->YEAR), sizeof(int), 1, arq) != 1 )
{
// Deal with the error condition.
}
必须将L->YEAR
的地址传递给fwrite
fwrite(L->YEAR, sizeof(int), 1, arq);
fwrite(&(L->YEAR), sizeof(int), 1, arq);
以下代码:
1) eliminates the bad practice of typedef'ing a struct
2) removed the mis-leading all caps of the struct field names
3) contains the needed #includes
4) contains the needed prototypes for the external (in another file) functions
5) checks for the worst of the many possible runtime errors
6) replaces the mis-leading 'L' with a meaningful name
7) modifies the file pointer variable to a commonly known name
suggest compiling with all warnings enabled (and fix the warnings)
for gcc, at a minimum, use '-Wall -Wextra -pednatic'
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Book
{
char *title;
char *author;
char *publisher;
int year;
char *language;
int pages;
float price;
};
int reglen( struct Book * );
int readData( struct Book * );
void writeRegister(FILE *fp, struct Book *myBook)
{ //writes in actual file position
char c = '|';
int sizeRegWrite = reglen(myBook); //reglen() returns the size of Book
fwrite(&sizeRegWrite, sizeof(int), 1, fp);
fwrite(myBook->title, sizeof(char), strlen(myBook->title), fp);
fwrite(&c, sizeof(char), 1, fp); //writing delimiter
fwrite(myBook->author, sizeof(char), strlen(myBook->author), fp);
fwrite(&c, sizeof(char), 1, fp); //writing delimiter
fwrite(myBook->publisher, sizeof(char), strlen(myBook->publisher), fp);
fwrite(&c, sizeof(char), 1, fp); //writing delimiter
fwrite(&(myBook->year), sizeof(int), 1, fp);
fwrite(&c, sizeof(char), 1, fp); //writing delimiter
fwrite(myBook->language, sizeof(char), strlen(myBook->language), fp);
fwrite(&c, sizeof(char), 1, fp); //writing delimiter
fwrite(&(myBook->pages), sizeof(int), 1, fp);
fwrite(&c, sizeof(char), 1, fp); //writing delimiter
fwrite(&(myBook->price), sizeof(float), 1, fp);
fwrite(&c, sizeof(char), 1, fp); //writing delimiter
return;
} // end function: writeRegister
int main( void )
{
FILE *fp = fopen("BD_books2.bin", "rb");
if(fp == NULL)
{
perror( "fopen for BD_books2.bin failed" );
exit( EXIT_FAILURE );
}
// implied else, fopen successful
struct Book myBook;
int goodRead = readData(&myBook); //Reads all fields from keyboard and places in Book. Working properly
if( goodRead )
{
writeRegister(fp, &myBook);
}
system("pause");
return 0;
} // end function: main