Thread: WIP Borland C Doubly linked list

  1. #16
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    fwrite(&item,sizeof(item),1,in);
    change it to write each node to file. So for example

    Code:
    fwrite(p,sizeof(item),1,in);
    This writes each node on to file. The first parameter is the actual node which u need to write and the next sizeof the node which u wanted to write. And next the no of nodes which u need to write in the above case its just one cos, you are writing just on node. And the last is the File pointer.

    And dont forget to close the file.

    ssharish

  2. #17
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    OK I decided to simlify the code and join save and view functions. Of course it will be wonderful if there was no any errors. I put compiller messages near the lines. What does it want from me? %|

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <alloc.h>
    #include <string.h>
    struct item
    {char name[20];int prc; int col;
      struct item *prev;
      struct item *next;
    };
    void build(void);
    void view(item *,FILE *in);
    struct item *head,*tail;
    main(){
    clrscr();
    build();
    view(head); //Too few parameters in call to 'view(item far*,struct far*)' in function main()
    }
    void build(void)
    {item *p,*pr;
      pr=NULL;
        do { p=(item *)new item;
        clrscr();
        printf("Name : ");
        scanf("%s",&p->name);
        printf("\nPrc : ");
        scanf("%d",&p->prc);
    
        printf("\nCol: ");
        scanf("%d",&p->col);
        p->prev=pr;
      if(pr != NULL)
        pr->next=p;
      else
        head=p;
        pr=p;
        printf("Esc");
    }
    while (getch()!=27);
        tail=p;
        tail->next=NULL;
    }
    void view(item *p,FILE *in)
    {int i=1;
    clrscr();
    in=fopen("data.txt","wb"); //Cannot assign 'unsigned int' to 'struct far*' in function view(item far*,struct far*)
      while (p != NULL)
      {printf("\n %-2d   %-15s\t%3d\t%3d",i,p->name, p->prc, p->col);
      in=fwrite(p, sizeof(item),1,in); //Parameter 'in' is never used in function view(item far*,struct far*)
      i++;
      p=p->next;
        getch();
    }
    }

  3. #18
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    #include <stdio.h>
    /* #include <conio.h> */
    #include <alloc.h>
    #include <string.h>
    #include <errno.h>
    
    struct item
    {
       char name[20];
       int prc,col;
       struct item *prev;
       struct item *next;
    };
    
    void build(void);
    void view(struct item *);
    
    struct item *head, *tail;
    
    int main()
    {
         /* clrscr(); */
        build();
        view(head); 
        
        getchar();
        return 0;
    }
    
    .
    .
    .
    .
    void view(item *p)
    {
         int i=1;
         FILE *in;
         
         /* This is a non standard function, DONT USE IT */
         clrscr();
         
         in=fopen("data.txt","wb"); 
         
         if( in != NULL )
         {     
             while (p != NULL)
             {
                   printf("\n &#37;-2d   %-15s\t%3d\t%3d",i,p->name, p->prc, p->col);
                   
                   if(!fwrite(p, sizeof(item),1,in))
                   {
                       perror("fwrite");
                       getchar();
                       fclose(in);
                       return; 
                   }
                   i++;
                   p = p->next;
             }
             fclose(in);
         }
         else
         {
             printf("Error: File cannot be created/opened\n");
             printf("Press any key to continue...\n");
             getchar();
             return;
         }
    }
    I will leave the build with you. Look through it, what error checking you need to do and what need passing as parameter.

    ssharish

  4. #19
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    Thanx a lot! After some corrections the code works the way i want. Now my last problem is loading from file. Of course I don't exactly know how to realize it. But something tells me that It just the same as a build a list but the function should take the data from saved file.
    Right or not? But what about memory? Should it be free?

    Code:
    void load(void)
    {item *p,*pr;
    FILE *in;
    in=fopen("data.txt","wb");
      pr=NULL;
        do { p=(item *)new item;
        fread(p,sizeof(item),1,in);
        p->prev=pr;}
        while (!feof(in))
      if(pr != NULL)
        pr->next=p;
      else
        head=p;
        pr=p;
    }
        tail=p;
        tail->next=NULL;
    }

  5. #20
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > Right or not?

    You aren't paying attention to what anyone is doing for you. A function like load() needs to return anything it builds up from nothing to its caller. Please, please return valid pointers to new lists. If you don't do that your implementation is broken, because anything that works will be pointless after load() leaks memory and you lose access to the entire data structure.

    > But what about memory? Should it be free?

    Yes it should. But you have to have access to the memory you allocate in the first place. Destroying any data structure allocated on the heap is an operation that needs to be done explictly in C. Say you had a routine

    void freelist( item *first );

    You can write freelist by walking through a list that was passed in by the first item and freeing nodes along the way.

    Only call freelist( first_item ) in load() if an error occurred and you need to clean up a half-completed list. Destroying the list in the load() routine for any other reason breaks your implementation.
    Last edited by whiteflags; 09-19-2007 at 12:21 AM.

  6. #21
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    having spended a couple of hours. I still have no idea . I've tryed to add free memory ability. It seems to me that I work in wrong direction.

    Code:
    void load(void)  //still haveing problems with parameters
    {item *p,*pr;   //I tryed to modify the build function
    FILE *in;         //Defines file
    while (p!=NULL){ //free memory
    p=p->next;
    free(p);
    }
    in=fopen("data.txt","wb");   //opens file
      pr=NULL;
        do
        {p=(item *)malloc(sizeof(item))
        p=fread(p,sizeof(item),1,in); //read one item from file.
        p->prev=pr;}
        while (!feof(in))
        }
      if(pr != NULL)
        pr->next=p;
      else
        head=p;
        pr=p;
    }
        tail=p;
        tail->next=NULL;
    }

  7. #22
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    Please can somebody help me? I have only a few days to compete my project.

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I take it the code you posted earlier doesn't compile - it has several missing semi-colon's, and two extraneous curly braces.

    When this bit of code starts, p is not initialized, so it will be some random garbage - freeing a "list" of random garbage is absolutely detrimental to the stability of the system.
    Code:
      while (p!=NULL){ //free memory
        p=p->next;
        free(p);
      }
    Code:
        p=fread(p,sizeof(item),1,in); //read one item from file.
    Setting p to the value read from the file is perhaps not such a good idea, as p is a pointer, and the return from "fread" is the number of items successfully read.

    You will probably also find that you attempt to read one more item than there is in the file. If you check the result from fread() [but not by putting the result into a pointer], you will have a more reliable way to ensure that your function doesn't read any more data than it should.

    You are not including the code to build the linked list in your loop to read the data from the file, you need to move it up to before the "while" end of your do-while-loop.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #24
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    This is my new version of load function. The last function I need to add to my project .More then one week passed And Im still moving in wrong direction.
    Can somebody help me!
    I might be stupid but im not lazy! And I have only 2 days to complete Please don't leave me.

    Code:
    void load()
    {item *p,*pr;
      pr=NULL;
      FILE *in
      in=fopen("data.txt","wb");
        do { p=(item *)new item;
        fread(p, sizeof(item), 1, in);
        scanf("&#37;s",&p->name);
        scanf("%d",&p->prc);
        scanf("%d",&p->col);
        p->prev=pr;
      if(pr != NULL)
        pr->next=p;
      else
        head=p;
        pr=p;
    }
    while (!feof(in));
        tail=p;
        tail->next=NULL;
    }
    Last edited by BCWarrior; 09-23-2007 at 12:28 AM.

  10. #25
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

  11. #26
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    Bad joke &#37;(

  12. #27
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    so you have one? did you try compiling it?

  13. #28
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    It to me seems that I missed the time when computers begun to do everything right without man.
    My problem Is that I have read lots of articles about lists and bin files And about methods of saving and reading. But I can't find any example about saving 2-linked lists. So I have a lot of blanks in my knowledge And I still cant fill them.

  14. #29
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    void load(head)
    {item *p *pr;
    FILE *in;
    in=fopen("data.txt","wb");
    fread(p, sizeof(item), 1, in);
    while(!feof(in))
    {scanf("
    void load()
    Does this mess even compile?

    Also, why are you trying to read from a file opened in "wb" mode?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #30
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    Quote Originally Posted by Salem View Post
    Code:
    void load(head)
    {item *p *pr;
    FILE *in;
    in=fopen("data.txt","wb");
    fread(p, sizeof(item), 1, in);
    while(!feof(in))
    {scanf("
    void load()
    Does this mess even compile?

    Also, why are you trying to read from a file opened in "wb" mode?
    Ive corrected the code. And remove that. This was a part of one of my thoughts about function load.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Small help on doubly linked list.
    By Depthcharge101 in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2007, 09:31 AM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM