WIP Borland C Doubly linked list

This is a discussion on WIP Borland C Doubly linked list within the C Programming forums, part of the General Programming Boards category; OK, but you're still reading from a file opened for writing > fread(p, sizeof(item), 1, in); > scanf("%s",&p->name); I thought ...

  1. #31
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    OK, but you're still reading from a file opened for writing

    > fread(p, sizeof(item), 1, in);
    > scanf("%s",&p->name);
    I thought you just read the information from the file, why are you also reading from the user, and overwriting what you read from the file?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  2. #32
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    Quote Originally Posted by Salem View Post
    OK, but you're still reading from a file opened for writing

    > fread(p, sizeof(item), 1, in);
    > scanf("%s",&p->name);
    I thought you just read the information from the file, why are you also reading from the user, and overwriting what you read from the file?
    According your notice i changed file mode to rb
    so now it looks like this
    Code:
    in=fopen("data.txt","rb");
        do { p=(item *)new item;
        fread(p, sizeof(item), 1, in);
    I still can't understand

    1.So I open file
    2.And read 1 structure sizeof(item) from file
    3. But what to do next?

    If im not mistaken struct item consist of data and pointers? so what to do with them? or they wasnt saved with data?
    I decided to use scanf. Is this right choice? Or the procedure shouold be different?
    I have too many questions but I can't find answers anythere

    Code:
      in=fopen("data.txt","rb");
        do { p=(item *)new item;
        fread(p, sizeof(item), 1, in);
        scanf("%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;
    }

  3. #33
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    I would suggest you make a clear distinction between reading the data (from whatever source) and placing that data in the list.
    Code:
    typedef struct {
        int data;
    } data;
    typedef struct list_tag {
        struct list_tag *next;
        data             data;
    } list;
    
    list *append ( list *oldlist, data data ) {
        list *newlist = malloc( sizeof *newlist );
        if ( newlist != NULL ) {
            newlist->data = data;
            newlist->next = NULL;
            if ( oldlist == NULL ) {
                /* a new list */
                oldlist = newlist;
            } else {
                /* non empty list, seek the tail and append */
                list *tail = oldlist;
                while ( tail->next != NULL ) tail = tail->next;
                tail->next = newlist;
            }
        }
        return oldlist;
    }
    
    list *readFromUser ( void ) {
        list *list = NULL;
        data  data;
        while ( scanf("%d",&data.data) == 1 ) {
            list = append( list, data );
        }
        return list;
    }
    
    list *readFromFile ( char *filename ) {
        FILE *fp = fopen( filename, "rb" );
        list *list = NULL;
        data  data;
        while ( fread( &data, sizeof data, 1, fp ) == 1 ) {
            list = append( list, data );
        }
        fclose( fp );
        return list;
    }
    Reading functions don't care how the detail of the list works.
    The list functions don't care where the data comes from.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #34
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    So I need to rewrite the add function to append data from user as well as from file?
    what does the list mean in your code?

  5. #35
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    List means the list you want to create.

    In main() say,
    list *mylist = readFromFile( "myfile.dat" );
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #36
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    btw is this single linked list?
    Oh

  7. #37
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Yes it is, but that doesn't change the overall approach.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #38
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    This code dose not produce any errors. It creates list and fills is, then saves to file and try to load. I tryed to printf the content loaded from file. Works fine. But is it podssible to do such a way

    read one structure from file and use it to fill new structure? till the end of file
    Also I wounder does my clear memory function work?
    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(struct item *);
    void load(void);
    struct item *head,*tail;
    main(){
    clrscr();
    build();
    view(head);
    getch();
    clrscr();
    printf("\nfrom file");
    load();
    view(head);
    getch();
    }
    void build(void)
    {item *p,*pr;
      pr=NULL;
    
        do { p=(item *)malloc(sizeof(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 load(void)
    {
    item *p,*pr;
    FILE *in;
    while(p!=NULL)
    {
    p=p->next; //Clears memory
    free(p);
    }
    in=fopen("data.txt","rb");
    do{p=(item *)malloc(sizeof(item));
      fread(p,sizeof(item),1,in);
     p->prev=pr;
      if(pr != NULL)
        pr->next=p;
      else
        head=p;
        pr=p;
        printf("Esc");
    }
    while (!feof(in));
        tail=p;
        tail->next=NULL;
    }
    
    void view(item *p)
    {
         int i=1;
         FILE *in;
         clrscr();
         in=fopen("data.txt","wb");
         if( in != NULL )
         {
             while (p != NULL)
             {
                   printf("\n %-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;
         }
    }

  9. #39
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    Or can you help me to change variables in your append and load functions to mine which i used in my code? Such as item p next prev etc.
    Please

  10. #40
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    I find your lack of braces and poor indentation too hard to read.

    But the loop calling free() is most definitely wrong.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #41
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    Can load from file function be realized such a way. If im not misstaken I read one structure from file to memory. Then if I want to prin it I can use something like this

    printf("\n %d %s\t%d\t%d",i,p->name, p->prc, p->col)

    so p->name, p->prc, p->col mean that I can read them from memory. But can I use them to create new item of the list? If yes how to get them an assign to new thread

    I though it can be realized such a way bout its just fought. Can somebody answer my questions?

    Code:
    void load(void)
    {item *p, *ni, *pr;
    FILE *in;
    p = head;
    while(p!=NULL)
    {
        item *pTemp;
        pTemp=p->next;
        free(p);
        p = pTemp;
    }
    in=fopen("data.txt","rb");
    do{
      ni=(item *)malloc(sizeof(item));
      fread(p,sizeof(item),1,in);
      p->name=ni->name;
      p->prc=ni->prc;
      p->col=ni->col;
      ni->prev=pr;
      if(pr != NULL)
        pr->next=p;
      else
        head=ni;
        pr=ni;
        printf("Esc");
    }
    while (!feof(in));
        tail=ni;
        tail->next=NULL;
    }

  12. #42
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > printf("\n &#37;d %s\t%d\t%d",i,p->name, p->prc, p->col)
    This ALWAYS reads from memory.
    It doesn't care how you wrote to that memory.

    You could do
    p->col = 0;

    Or
    scanf("%d", &p->col );

    Or
    fread( .... )

    Or
    p->col = aFunctionReturningInt();

    It's all the same.



    ni=(item *)malloc(sizeof(item));
    fread(p,sizeof(item),1,in);
    p->name=ni->name;
    p->prc=ni->prc;
    p->col=ni->col;
    ni->prev=pr;
    This is the wrong way round.
    You read into where p points, then you OVERWRITE p with the uninitialised junk in ni.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 3 of 3 FirstFirst 123
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, 08:31 AM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 12: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, 01:46 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21