fwrite() not working to write integer in binary file

后端 未结 4 969
暖寄归人
暖寄归人 2021-01-16 23:25

Can someone tell my why won\'t this function work? I just can\'t get it...

void writeRegister(FILE *arq, Book *L){ //writes in actual file position
  char          


        
相关标签:
4条回答
  • 2021-01-17 00:02

    You must pass the address of L->YEAR to fwrite

    fwrite(L->YEAR, sizeof(int), 1, arq);
    
    fwrite(&(L->YEAR), sizeof(int), 1, arq);
    
    0 讨论(0)
  • 2021-01-17 00:05

    the following code:

    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
    
    0 讨论(0)
  • 2021-01-17 00:07

    The first parameter of fwrite expects a pointer.

    Lines such as the following:

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

    Should be written as follows:

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

    Sames goes for YEAR and PRICE members of that struct

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

    Note, you don't need to make the same change for TITLE, PUBLISHER, and AUTHOR because the type of those member fields are already pointers (char*).

    0 讨论(0)
  • 2021-01-17 00:20

    The signature of fwrite is

    std::size_t fwrite( const void* buffer, std::size_t size, std::size_t count, std::FILE* stream );
    

    The first argument to the function needs to be a pointer. I am surprised you didn't get compiler errors with the following lines.

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

    They need to be

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

    Also, it is a good practice to check the return values of all IO functions to make sure that they work as you expect them to.

    if ( fwrite(&(L->YEAR), sizeof(int), 1, arq) != 1 )
    {
       // Deal with the error condition.
    }
    
    0 讨论(0)
提交回复
热议问题