fwrite()无法在二进制文件中写入整数

时间:2015-06-01 04:30:08

标签: c file binary fwrite

有人可以告诉我为什么这个功能不起作用?我只是无法得到它......

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;
}  

struct 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;

}

我必须在结构中使用那些指针,所以我无法删除它们。顺便说一下,我的问题只出在那些integersfloat上。

如果我用fprintf()写所有的整数和浮点数(年份,页数和价格),这个函数只能正常工作,但是我用二进制文件写它,当然我想写它在二进制中,所以我试图使用fwrite()。

另一件事是:编译器将incompatible type for argument 1 of 'fwrite'指向此行:fwrite(L->PRICE, sizeof(float), 1, arq);

有人可以解释一下发生了什么吗? 我的程序在尝试写入文件时崩溃...

4 个答案:

答案 0 :(得分:6)

fwrite的第一个参数需要一个指针。

以下行:

fwrite(&(L->PAGES), sizeof(int), 1, arq);

应写成如下:

fwrite(&(L->YEAR), sizeof(int), 1, arq);
...
fwrite(&(L->PRICE), sizeof(float), 1, arq);

Sames适用于该结构的YEAR和PRICE成员

g

注意,您不需要对TITLE,PUBLISHER和AUTHOR进行相同的更改,因为这些成员字段的类型已经是指针(char *)。

答案 1 :(得分:2)

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.
}

答案 2 :(得分:1)

您必须将L->YEAR的地址传递给fwrite

fwrite(L->YEAR, sizeof(int), 1, arq);

fwrite(&(L->YEAR), sizeof(int), 1, arq);

答案 3 :(得分:0)

以下代码:

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