Thread: linked list and files...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    37

    linked list and files...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct node nodeType; //data structure
    
    struct node
    {
      char name;
      nodeType *next;
    };
    
    //adds a new character in the linked list with each character read.
    nodeType*
    chinsert(nodeType* ptr,char c)
    {
      nodeType *p;
    
      nodeType *new = (nodeType *)(malloc(sizeof(struct node))); //malloc to create the memory space that's needed to store the new character.
      new->name = c;
      new->next = NULL;
    
      if(ptr==NULL)
        return new;
      
      else
      {
        for(p=ptr; p->next!=NULL; p=p->next);
        
        p->next= new;
    
        return ptr;
      }
    }
    
    //When EOF is detected, traverse the linked list printing out the list of characters stored in the list
    void
    prnList(nodeType *ptr)
    {
      nodeType *n = ptr;
    
      printf("\n");
      
      while(n != NULL)
      {
        printf("%c",n->name);
        n = n->next;
      }
      
      printf("\n");
      
    }
    
    //add underscore replacing space key
    void
    prn(nodeType *ptr)
    {
      nodeType *n = ptr;
    
      while(n != NULL)
      {
        if(n->name == ' ')
          printf("%c",n->name='_');
    
        else
          printf("%c",n->name);
    
        n = n->next;
      }
    
      printf("\n");
    
    }
    
    //return any memory used back to the system before to the system before exiting program.
    void
    cleanUp(nodeType *ptr)
    {
      nodeType *n;
    
      while(ptr->next != NULL)
      {
     
        n = ptr->next;
        ptr->next = n->next;
        free(n);
      }
    
       n = ptr;
       free(n);
    }
    
    int
    main(int argc, char *argv[])
    {
      char c;
      nodeType *ptr = NULL;
    
      while((c=getchar())!=EOF) //loop that reads in a character at a time, terminating the loop when it sees an EOF.
      {
        ptr = chinsert(ptr,c);
      }
    
      prnList(ptr);
      prn(ptr);
      cleanUp(ptr);
    
      return 0;
    }
    this program uses linked list to print the input replacing the space key with underscore.

    how do i modify this program so it takes lines from a file?
    do i just replace getchar with fgets?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Something like that. There are many ways to do it. The basic concept is:
    Code:
    open file
    while not done
        read something from file
        process input
    close file when done with it
    If you're not sure, make a program for starters that just opens a file, reads a chunk (line, character, whatever) and displays that on the screen. The getchar replacement would be fgetc. fgets reads blocks of information, specificly strings or lines.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    37
    is it something like this?
    Code:
    void
    prnL(nodeType *ptr)
    {
      char c;
      FILE *fp=ptr;
      while (1){
        c=fgetc(fopen(argv[1],"r"));
        print("%c", fp)
          fp=fp->next;
      }
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by 8ball
    is it something like this?
    Code:
    void
    prnL(nodeType *ptr)
    {
      char c;
      FILE *fp=ptr;
      while (1){
        c=fgetc(fopen(argv[1],"r"));
        print("%c", fp)
          fp=fp->next;
      }
    }
    No. *cringe*

    1) You don't assign a node pointer to a file pointer.
    2) You don't call fopen every single time you read a character.
    3) You don't advance file pointers like that. (I know, you're actually meaning to advance through your list, but it doesn't work that way...you can't just advance down a file.)

    If you wanted to do what I'll guess you're trying to do here, it would be:
    Code:
    void
    prnL(nodeType *ptr)
    {
        int c;
        nodeType *node = ptr;
        FILE *fp=NULL;
    
        fp = fopen( argv[1], "r" );
        if( fp != NULL )
        {
            while( (c=fgetc( fp )) != EOF && node != NULL )
            {
                node->something = c; /* assign this character to an existing node */
                node = node->next;
            }
            fclose( fp );
    }
    And even that is not good code. I mean, I suppose it would work, but it's hardly good.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM