c - (C) 将std in解析成链表,无限循环

标签 c scanf

出于某种原因,在 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 = '\0';
    if (1 != sscanf(cptr, "%255s", newnode->fileName))
      assert(0), abort();

    cptr = eptr + 1;

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

    *eptr = '\0';
    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 个字符。

关于c - (C) 将std in解析成链表,无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28575915/

相关文章:

php - 使用哈希表访问 C 中的 PHP 变量 - 指针间接级别的更改

c - scanf(),字段宽度,inf 和 nan

python - 在第一原则级别将 float 转换为 bool 值或从 bool 值转换时会发生什么?

C - 使用 sscanf 读取多个整数和 float

c - 将输入发送到 C 程序并打印它们

c - 我的 fscanf 输出一个很大的负数

c - Stdin 结构问题

c++ - 有没有办法排队线程?

c - pthread 的堆栈大小如何影响内存使用?

c - 这段代码的输出是什么意思? printf ("%d", "x");