FPUTS-struct node loop.

This is a discussion on FPUTS-struct node loop. within the C Programming forums, part of the General Programming Boards category; I have to make a programm that among others outputs a text stored in a struct node (line by line), ...

  1. #1
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32

    FPUTS-struct node loop.

    I have to make a programm that among others outputs a text stored in a struct node (line by line), in a file. I use fputs.
    Here is the function PrintFile that prints the file:
    Code:
    void WriteFile(struct list *buffer, char *filename)             
    {
            char line[MAXLINE];
            FILE *fp;
                            fp=fopen(filename,"w");
                            if (fp==NULL)
                            {
                                    printf("Den einai dinaton, na ginei eggrafi sto arxeio %s!", filename);
                            }
                            else
                            {
                                    int currentpos;
                                    struct node *nodeM;
                                    int *current;
    
                                    fputs(line, fp);
    
                            }
                            fclose(fp);
    }
    This function just prints the last line of the text. I want to make a loop that puts each line from the first to the last, but I cant. I tried a for and a while, I put a j as the number of the line but it didn't work either. It can't be compiled or I get a segmentation fault when running.
    Maybe there is something wrong with the rest of the programm! If you want some other part of the code in order to help me-I will send it, I just didn't want to add 280 lines of code for a function.
    Thanks,
    AA
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Code:
    while ( p != NULL ) {
      /* do stuff with node p */
      p = p->next;
    }
    This is very typical how how you traverse a linked list.
    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.

  3. #3
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32
    I did it but with prev instead of next as it starts from the last line, and I could not make the *next pointer while reading the file. I think it should work but : segmentation fault! I HATE SEGMENTATION FAULTS!!!
    I don't know why, it doesnt work I have tryed a lot of simmilar things wich I found on codes while searching this forum but nothing works.
    What do you think? Maybe I don't store the file properly???
    Here is the function that reads the file:
    Code:
    void ReadFile(struct list *buffer, char *filename)    
    {
            char line[MAXLINE];
                            struct node *nodetmp;
    
    
                    FILE *fp;
                    fp = fopen(filename,"r");
                    if (fp==NULL)
                            printf("The file %s can't be read!", filename);
                    else
                    {       int size, currentpos;
                            struct node *nodeM;
    
    //                      buffer->head=fgets(line,MAXLINE,fp);     //this is for *head! but it doesnt work!
    
                            while ((fgets(line, MAXLINE, fp))!=NULL)
                            {
                                    nodeM=MALLOC(sizeof(struct node));
    
                                    ++currentpos;
                                    ++size;
                                    nodeM->prev = buffer->tail;
    //                              nodetmp = (*buffer).tail;             // this 2 lines are for next but when thy are not as a coment I get segmentation fault while reading
    //                              (*nodetmp).next=nodeM;
                                    buffer->tail=nodeM;
    
                                    printf("%s",line);   //actually I don't need this line, but its to see the file while reading
                            }
                    }
                    fclose(fp);
    }
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    > buffer->head=fgets(line,MAXLINE,fp); //this is for *head! but it doesnt work!
    Because ALL your buffers end up pointing at the SAME LOCAL variable!
    You fgets() into the local variable, then you allocate if necessary your buffer->head, then you strcpy() into it.

    head is a very poor choice of name for actually storing data.

    Please post your struct list next time.
    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.

  5. #5
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32
    These are my structs:
    Code:
    struct node {
            char         *line;
            struct node  *prev;
            struct node  *next;
    };
    
    struct list {
            struct node  *head;
            struct node  *tail;
            int           size;
            int     currentpos;
            char      *current;
    };
    head is the pointer to the first node and tail to the last. Why is it poor? Actually this is a project and it has to be made with this structs.
    By the way I have starting working on this exactly one week before, and work on it whenever I have free time, but still only 2 of 7 functions are made and these do not work prperly too. I have now 3 days left and I am getting stress. So I would be glad if somebody helped me. I have read the chapter about pointers in the K&R book 3 times, but this doesn't help. I also looked at the tuts here but I cant make the functions work !
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    > head is the pointer to the first node and tail to the last. Why is it poor?
    > buffer->head=fgets(line,MAXLINE,fp);
    See the answer yet?

    Try this
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    struct node {
      char         *line;
      struct node  *prev;
      struct node  *next;
    };
    
    struct list {
      struct node  *head;
      struct node  *tail;
      int           size;
      int     currentpos;
      char      *current;
    };
    
    struct node *makeNode ( char *line ) {
      struct node *new = malloc ( sizeof *new );
      if ( new ) {
        /* initialise a node */
        new->prev = NULL;
        new->next = NULL;
        new->line = malloc ( strlen(line) + 1 );  /* should check NULL */
        strcpy ( new->line, line );
      }
      return new;
    }
    
    void appendlist ( struct list *list, char *line ) {
      struct node *new = makeNode ( line );
      /* now add it to the list */
      if ( list->head == NULL ) {
        /* easy, list is empty */
        list->head = new;
        list->tail = new;
        list->size = 1;
      } else {
        /* fix the tail node of the list */
        list->tail->next = new;
        /* fix the previous of the new node */
        new->prev = list->tail;
        /* move the tail forward */
        list->tail = new;
        list->size++;
      }
    }
    
    void ReadFile ( struct list *list, char *filename ) {
      char buff[BUFSIZ];
      FILE *fp = fopen( filename, "r" );
      while ( fgets ( buff, BUFSIZ, fp ) != NULL ) {
        appendlist ( list, buff );
      }
      fclose ( fp );
    }
    
    void WriteFile ( struct list *list, char *filename ) {
      FILE *fp = fopen( filename, "w" );
      struct node *p = list->head;
      printf( "List as %d nodes\n", list->size );
      while ( p != NULL ) {
        fputs ( p->line, fp );
        p = p->next;
      }
      fclose ( fp );
    }
    
    int main ( ) {
      struct list mylist = { 0, 0, 0, 0, 0 }; /* empty list */
      ReadFile ( &mylist, "foo.c" );
      WriteFile ( &mylist, "bar.c" );
      return 0;
    }
    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.

  7. #7
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32
    Salem! I don't know how to thank you.
    Really thanks a lot.
    It works, I have been a lot further with my project(thanks to your help I could make 2 more functions: InsertLine and DeleteLine also work perfect now, and the most important i finally understood (or I think so) how linked lists work.
    I can't describe how happy I am!
    Thanks again.
    I owe you one!
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked list acting as string
    By drater in forum C Programming
    Replies: 30
    Last Post: 04-30-2008, 07:10 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Again, Circular Linked list ???
    By yescha in forum C Programming
    Replies: 2
    Last Post: 11-16-2001, 07:35 PM

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