Reading Text file in Linked list!

This is a discussion on Reading Text file in Linked list! within the C Programming forums, part of the General Programming Boards category; Hello ! I have a text file of strings like; kumar sinha sinha kapoor gupta I want to read this ...

  1. #1
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96

    Reading Text file in Linked list!

    Hello !
    I have a text file of strings like;

    kumar
    sinha
    sinha
    kapoor
    gupta

    I want to read this into a linked list and want to remove the duplicates.
    My problem is I dont know how to read it in Linked list.

    Please suggest something here!
    Thanks.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Do you know how to implement a linked list?

    With say methods such as
    - add
    - find
    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++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    Yes , I know how to implement Linked list for the finding and removing the duplicates for integers but have problems with strings!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So use strcmp instead of == to compare strings instead of numbers. Everything else is the same.


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

  5. #5
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    Even that fine, but the primary issue is with importing this file in linked list directly.
    I mean I can read it an array but i wanna do it with Linked list .

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So read it into an array, and copy that into your linked list node. Then go on to the next record. I thought you said you already knew how to do a linked list with numbers? Ok, so, store your record instead of a number. What's the problem?


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

  7. #7
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    my code looks like

    Code:
    main ()
    {
    
    FILE *fp;
     fp = fopen ("Names.txt","r");
     int i, names =0;
     char c, *pN;
     while((c=getc(fp))!=EOF) 
       {
         if(c=='\n')
           names++                
    	 }
     printf("\tThe total number of members are : %d\n",names);   
     
     
     pN = (char*) calloc(names, sizeof(char));
     rewind (fp1);
     
     for (i=0;i<names;i++)
       {
         fscanf(fp,"%s", &pN[i]);
         printf("%s", pN[i]);
         
       }

    I am able to the file , but no clue how to change it to Linked list.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Like I said, what part don't you know how to do? Do you know how to make a linked list? You said you did. So, write some code that makes a linked list, then worry about filling it.


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

  9. #9
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    ok, I tried but doesnt work....
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    struct node {
      char data;
       struct node *next;
    };
    
    typedef struct node *item;
    
    main ()
    {
    FILE *fp;
     fp = fopen ("Names.txt","r");
     int i, names =0;
     char c, *pN;
     while((c=getc(fp))!=EOF) 
       {
         if(c=='\n')
           names++ ;               
    	 }
     printf("\tThe total number of members are : %d\n",names);   
     
     
     pN = (char*) calloc(names, sizeof(char));
     rewind (fp);
     
     for (i=0;i<names;i++)
       {
         fscanf(fp,"%c", &pN[i]);
         printf("%c", pN[i]);
         
       }
    
    item  curr,  head = NULL,  tail = NULL;
    
     head = NULL;
    for(i=1;i<=names;i++) {
          curr = (item *)malloc(sizeof(item));
          curr->data = pN[i];
          curr->next  = tail;
          tail = curr;
      if (!head) head = curr;
       }
    
       curr = head;
    
       while(curr) {
          printf("%d\n", curr->data);
          curr = curr->next ;
       }
    }

    Also, the strings are captured as Characters,
    i mean the file is printed as

    S
    i
    n
    h
    a

    k
    a
    p
    o
    o
    r


    n Am I copying the array in Linked list correctly.
    thanks.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are you just trying to store one character in a node, or a word? If you want a word, you want an array of character in your structure, not just a single char. And, if that's the case, then you can use something like fgets instead of fgetc. Or you can change up fscanf to read a word instead of a single character.


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

  11. #11
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    I had tried that too..
    but then i get a warning like

    warning: this program uses gets(), which is unsafe.

    and then there is no output.
    Yes, I want to store the word.
    n When u say, an array of characters in structure...

    then would be something like
    Code:
    struct node {
      char data[10] ;
       struct node *next;
    };

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    Notice that quzah mentioned (and even highlighted) fgets, not gets.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    Ohh !
    yes, I did that.
    I am able to read the whole string but in the following way...
    Code:
    FILE *fp;
      char line[LINE_LENGTH];
      int count=0,i;
      
     char names[10];
     fp = fopen ("Names.txt","r");
     while(feof(fp)==0) 
       {
         fgets(names,10,fp)  ;
         count++;
         printf("%s",names);
       }	
     printf("File contains %d lines.\n", count-1);
      
      
    fclose(fp);
    But I still am not able to get it into a linked list.
    though i tried it something like this
    Code:
     head = malloc(sizeof(struct node));
     for(i=1;i<=count;i++) {
       
       Push(&head,names[i]);
     }
     printf("%s", head);
    have no idea , if it is correct because there seems some issue with "Push".
    can u plz help me with some code to make it linked list.
    thanks.

  14. #14
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96

    Unhappy

    i wrote a function to remove the duplicates but i get the Segmentation error


    Code:
    removeDuplicates(struct node* head)
    {
     struct node* current = head;
     struct node* next_next;
     while(current->next != NULL)
     if (strcmp (current->data, current->next->data) ==0)
        {
        next_next = current->next->next;
         free(current->next);
         current->next = next_next;
        }
       {
          current = current->next;
        }
      }
    }
    While my main function looks like:

    Code:
    int main ()
    {
    
     FILE *fp;
     int i;
     char line[LINE_LENGTH];
     int count = 0;
    char names[10];
     fp = fopen ("Workbook2.txt","r");
    while(feof(fp)==0) 
      {
         gets(names,10,fp)  ;
        count++;
        //printf("%s",names);
       }
     printf("%d\n", count-1);
     struct node* head = NULL;
     for(i = 0; i <count;i++)
       {
         push(&head,names[i]);
       }
     removeDuplicates(head);
      fclose(fp);
     }

    Am i doing something really stupid here!??

  15. #15
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Question 12.2
    You don't need array to store them temporarily. Isn't the point of linked list!
    Pseudo Code.
    Code:
    while( Not EOF ) {
       add( list , data );
    } 
       removeDuplicate(list);
       printList(list);
    Last edited by Bayint Naung; 07-28-2010 at 07:14 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  3. Why won't this work? (Linked List and text file)
    By omishompi in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2005, 11:24 AM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 03:49 PM
  5. Reading a file into a Linked List
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-20-2002, 07:08 AM

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