possible problem with fgetc?????

This is a discussion on possible problem with fgetc????? within the C Programming forums, part of the General Programming Boards category; Each time I call this function for some reason I get a core dump. I've localized it to at least ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    2

    possible problem with fgetc?????

    Each time I call this function for some reason I get a core dump. I've localized it to at least the
    temp = fgetc(line); command but am not sure why it is happening. there more than liekly are other errors, hehe. But I'm sure I can get the figured out if I have some help on this one. Thanks!

    insert
    Code:
    void printline(FILE* line)
    {
      char temp;
      listptr head = NULL;
      listptr newNode;
      listptr tempNode;
    
      newNode = (listptr)malloc(sizeof(struct list));
    
      temp = fgetc(line);
    
      newNode->next = NULL;
      newNode->letter = temp;
    
      head->next = newNode;
    
      while((temp = fgetc(line)) != '\n')
        {
          if(temp == EOF)
            return;
    
          newNode->letter = temp;
    
          tempNode = newNode;
          newNode = tempNode->next;
        }
    
      tempNode = head->next;
    
      while(tempNode != NULL)
        {
          printf("%c", tempNode->letter);
    
          newNode = tempNode;
          tempNode = newNode->next;
        }
      return;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I've localized it to at least the temp = fgetc(line);
    Is the file open?

    >head->next = newNode;
    head is null.

    >newNode = tempNode->next;
    newNode is now null. The next iteration will bomb.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    2
    Yes the file is opened in another function and the file pointer is passed in to this function, which should print a line in the file with an undetermined number of characters which end with \n.
    I'm not entirely worried about the linked list, I can figure that out. I'm just not sure why everytime I use fgetc I get a core dump.

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by iamvarg
    Yes the file is opened in another function and the file pointer is passed in to this function, which should print a line in the file with an undetermined number of characters which end with \n.
    I'm not entirely worried about the linked list, I can figure that out. I'm just not sure why everytime I use fgetc I get a core dump.
    Prelude already answered your question. You probably aren't dumping at the fgetc(), it is more likely the NULL ref.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    > I'm just not sure why everytime I use fgetc I get a core dump.
    Because your linked list is a mess - as already stated.

    Separate the "read the file" from "append to list" functionality.

    So you have for example
    Code:
    list = NULL;
    while ( (ch=fgetc(line)) != '\n' ) {
      list = appendToList ( list, ch );
    }
    Then it becomes much easier to verify which is actually broken - the file reading, or the list management.

    It also becomes possible to test the list without the file, like
    Code:
    list = NULL;
    list = appendToList ( list, 'a' );
    list = appendToList ( list, 'b' );
    If that simple test fails, then work on the list, safe in the knowledge that it has nothing to do with the file handling.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

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