linked list with binary files

Discussion in 'C' started by george61, Sep 6, 2010.

  1. george61

    george61 New Member

    Joined:
    Sep 6, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Ok I have a linked list which have to read from binary file and to write in binary files.
    This is the function about the reading from keyboard:
    Code:
    short readElement( Node * newE)
    {         int b,e,m;
              if ( newE == NULL ) return 1;
              do {
                    printf("\n Registration number:");
                            scanf("%s", &newE->regnomer);
                    printf("\n Brand:");
                            scanf("%s", &newE->marka);
                    printf("\n Year:");
                            b = scanf("%d",  &newE->godina );
                    printf("\n Kilometres:");
                            e = scanf("%d",  &newE->kilometri );
                    printf("\n Passengers:");
                                            m = scanf("%d",  &newE->mesta );
                    fflush(stdin);
                    if ( b == EOF ) return 1;
                    if(dostovernost(newE))
                            printf("\nWrong input!");
            } while(dostovernost(newE));
            writebinary(newE,fout);
            return 0;
    }
    writebinary is the function about writing the binary file:
    Code:
    short writebinary(Node *curE,FILE *fout)
    {
            int k,l,m,n,p;
            if ( curE == NULL ) return 1;
            if ( fout == NULL) return 2;
            k = fwrite( &curE->regnomer, sizeof(Node), 1, fout );
            l = fwrite( &curE->marka, sizeof(Node), 1, fout );
            m = fwrite( &curE->godina, sizeof(Node), 1, fout );
            n = fwrite( &curE->kilometri, sizeof(Node), 1, fout );
            p = fwrite( &curE->mesta, sizeof(Node), 1, fout );
            if ( k == 1 && l==1 && m==1 && n==1 && p==1 ) return 0;
            return 3;
    }
    After testing the program it gaves a binary file with some size but I'm not sure the function is absolutely right.
    This is the file pointer FILE *fout=fopen("binar.dat","w");I'll be happy to be given some code about reading the completed binary file in humanly readable format, and to have function which reads binary data in the linked list. My last question is about the given code. Is it right? Best regards.
     
  2. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    http://www.techgeek.in
    Generally, the functions fread() and fwrite() are the easiest to use for manipulating binary data.
    To write an entire linked list, you will want to write each node individually, before traversing it to the next node in the list.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice