Thread: Reading Text file in Linked list!

  1. #16
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    ok..
    I dont get segmentation fault now...
    but nothing is printed either
    any suggestion why???
    Last edited by satty; 07-28-2010 at 07:33 AM.

  2. #17
    Registered User
    Join Date
    May 2010
    Posts
    74
    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;
        }
      }
    }
    Somethings you could do, first specify a return type to the function, void if you don't want to return anything.
    Also, your while isn't working that well, you're not advancing the pointers. The while only works on the if statement.

    You could try using two pointers, one that points to the current, and the other to the previous, its easier to work with, and you can't forget to move the pointers forward in the list.

  3. #18
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    Thanks, I had made the changes u suggested but I dont know if M passing the strings correctly.

    1. I read the text file.
    2. Found the number of strings present
    3. printed the file
    4. tried [assing it to the function to remove duplicates

    Code:
    fp = fopen ("Names.txt","r");
     while(feof(fp)==0) 
       {
         fgets(names,10,fp)  ;
         count++;
         // printf("%s\n",names);
         
       }
     
     printf("%d\n", count-1);
     struct node* head ;
    
     while(!EOF)
       {
         add(&head,*names);
       }

    can u please go through this code!

  4. #19
    Registered User
    Join Date
    May 2010
    Posts
    74
    First test the variable 'fp' for NULL, just in case it didn't open.
    Code:
    if (fp == NULL)
    	{
    	//do something
    	}
    Then, you shouldn't use the feof() function to test if you reach the end of file.
    Try something like,
    Code:
    while(fgets(names , sizeof(names) , fp) != NULL)
    	{
    	count++;
    	//add names to linked list
    	}

  5. #20
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    Thanks a lot.
    I did the way u mentioned.
    It complies without error but doesnt give the result.I mean i get something like this
    List after removal of the duplicates = NULL.

    Code:
    struct node* head ;
     head = calloc(count,sizeof (char));
    if(fp ==NULL)
       {
         printf("Error");
       }
     else{
    
         while(fgets(names,10,fp) != NULL)
       {
         
         count++;
        printf("%s",names);
         add(head,*names);
       }
     } 
     printf("\n%d\n", count);
    any suggestion here!??

  6. #21
    Registered User
    Join Date
    May 2010
    Posts
    74
    when you send the string 'names', you want to send the pointer to the first element, like: add(head , names);

    You could also have some problem with the head pointer. I don't know how you wrote the add() function, can you show it?

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, 12:24 PM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04: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