(C) 将std in解析成链表,死循环

(C) parsing std in into a linked list, infinite loop

出于某种原因,代码在 for 循环接近尾声的第一个 printf 之后一直出现段错误。它根本不打印第二个 printf,有人能指出我正确的方向吗?我认为这与内存中的问题有关

struct node{
    char *fullString;
    char fileName[1024];
    int  lineNumber;
    char filler[1024];
    struct node *next;
}; 

int main (int argc, char *argv[])
{

    char tmpstring[1024];
    /* This won't change, or we would lose the list in memory */
    struct node *head = NULL;       
    //tail
    struct node *tail = NULL;  
    /* This will point to each node as it traverses the list */
    struct node *ptr, *newnode;  

    while (fgets(tmpstring, 1024, stdin) == tmpstring) 
    {
      if ((newnode = (struct node *)malloc(sizeof(struct node))) == NULL) {
        fprintf(stderr,"mklist: malloc failure for newnode\n");
        exit(1);
        }
        if ((newnode->fullString = malloc(strlen(tmpstring)+1)) == NULL) {
        fprintf(stderr,"mklist: malloc failure for newnode->str\n");
        exit(1);
        }
        if (strncpy(newnode->fullString, tmpstring, strlen(tmpstring)+1) != newnode->fullString) {
        fprintf(stderr,"mklist: string copy problem\n");
        exit(1);
        }

      strcpy(newnode->fullString, tmpstring);
      newnode->next = NULL;

//      if (3 != scanf(tmpstring, "%255s:%d:%255s", newnode->fileName, &newnode->lineNumber, newnode->filler))
 //       abort();

      if (tail == NULL)
        head = tail = newnode;
      else {
        tail->next = newnode;
        tail = newnode;
      }
    }

    // Now print the list, element by element.
    for (ptr = head; ptr != NULL; ptr = ptr->next)
        printf("%s\n", ptr->fullString);
        printf("%s:%d:%s\n", ptr->fileName, ptr->lineNumber, ptr->filler);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define MAX_LINE_SIZE 1024

struct node
{
  char fullString[MAX_LINE_SIZE];
  char fileName[MAX_LINE_SIZE];
  int  lineNumber;
  char filler[MAX_LINE_SIZE];

  struct node *next;
};  

int main (int argc, char **argv)
{
  char tmpstring[MAX_LINE_SIZE];
  struct node *head = NULL;
  struct node *tail = NULL;
  struct node *ptr, *newnode;  

  while (fgets(tmpstring, MAX_LINE_SIZE, stdin)) 
  {
    size_t len = strlen(tmpstring);
    char *cptr = tmpstring, *eptr;

    if (0 == len || tmpstring[len - 1] != '\n')
      assert(0), abort();  /* NOTE: will abort on too long lines and if EOF occurs mid-line */

    if (NULL == (newnode = calloc(1, sizeof(struct node))))
      assert(0), abort();

    strcpy(newnode->fullString, tmpstring);
    newnode->lineNumber = -1;
    newnode->next = NULL;

    if (NULL == (eptr = strchr(cptr, ':')))
      assert(0), abort();

    *eptr = '[=10=]';
    if (1 != sscanf(cptr, "%255s", newnode->fileName))
      assert(0), abort();

    cptr = eptr + 1;

    if (NULL == (eptr = strchr(cptr, ':')))
      assert(0), abort();

    *eptr = '[=10=]';
    if (1 != sscanf(cptr, "%d", &newnode->lineNumber))
      assert(0), abort();

    cptr = eptr + 1;
    strcpy(newnode->filler, cptr);

    if (tail == NULL)
      head = tail = newnode;
    else {
      tail->next = newnode;
      tail = newnode;
    }
  }

  for (ptr = head; ptr != NULL; ptr = ptr->next)
    printf("%s:%d:%s", ptr->fileName, ptr->lineNumber, ptr->filler);    

  return 0;
}

添加一些更好的错误处理,这可能就是您所追求的——只要您输入的行不超过 MAX_LINE_SIZE-1 个字符。