【问题标题】:Accessing to *head and reading from file访问 *head 并从文件中读取
【发布时间】:2015-05-07 11:23:15
【问题描述】:

我正在编写一个按字母顺序对文件中的行进行排序的程序。 我想通过将排序添加到动态分配的单向列表来做到这一点。 为了测试,我制作了一个程序,它从文件中读取fgetschar bufor[] 的行,然后我想将其添加到单向列表的开头,但是当我想检查*head!=NULL 时,然后我收到一个错误。问题出在哪里?

代码如下: 我已经做了结构

 typedef struct LISTA lista;
    struct LISTA
    { char*line;
    lista* next;};

void check(int argc,char * argv[]) {
    if (argc < 2)
    {
      fprintf (stderr, "Uzycie: %s nazwa_pliku\n", argv[0]);
      exit (-1);
    } }
FILE* otworz_plikR(char* namef) // Otwieranie pliku w Trybie READ
{
    FILE* fp;
    fp = fopen(namef, "r");
    if (fp == NULL) {
        fprintf(stderr, "Cannot open source file!\n");
        exit(1);
    }
    else printf("Source file opened!\n");
    return fp;
}
lista* stworz_liste(char* namef) 
{
    lista **head=NULL;
    char bufor[100];
    char *line;
    lista *tmp;

    *head=NULL;

    size_t* len = 0;

    FILE* fp=otworz_plikR(namef);

    while(fgets(bufor,&len,fp)!=NULL)

    {
        line = (char*)malloc(sizeof(char)* (strlen(&bufor) + 1));

        strcpy(line, bufor);  

        printf("%s",line);

        tmp = (lista*)malloc(sizeof(lista));

        tmp->next=NULL;

        tmp->line=line;

        if (*head != NULL) tmp->next = *head;

        *head = tmp;
    }
    fclose(fp);
    free (bufor);
    free (len);
    fclose(fp);
}

这是主要的:

int main( int argc, char * argv[] )
{
    check(argc,argv);
    lista *head=stworz_liste(argv[5]);
    printf("\n%s",head->line);
    free (head);
    return 0;
}

感谢您的帮助。 argv[3] = 输出.txt argv[5] = input.txt。 也许您还有其他问题可以解决这个问题?

【问题讨论】:

  • lista **head=NULL。这使得head NULL。所以你不能取消引用它。也就是说,*head 正在尝试取消引用空指针。
  • 0) stworz_liste 不返回值。 1) fgets(bufor,&amp;len,fp) --> fgets(bufor, sizeof bufor, fp)
  • 2) free (bufor); free (len); 这个删除。

标签: c list file head


【解决方案1】:

我已经简化了您的列表生成器功能,但添加了更多错误检查。方法是创建每个新节点,将其链接到现有列表,并使其成为列表的新头。最后返回列表指针。

lista *stworz_liste(char *namef) 
{
    lista *head = NULL;                                 // single indirection
    char bufor[100];
    lista *tmp;
    FILE* fp=otworz_plikR(namef);
    while(fgets(bufor, sizeof(bufor), fp) != NULL)      // replaced len
    {
        bufor [ strcspn(bufor, "\r\n") ] = 0;           // remove trailing newline
        tmp = malloc(sizeof(lista));                    // memory for list item
        if (tmp == NULL)
            exit(1);                                    // error
        tmp->line = malloc(strlen(bufor) + 1);          // simplify, remove &
        if (tmp->line == NULL)
            exit(1);                                    // error
        strcpy(tmp->line, bufor);  
        printf("%s\n",tmp->line);
        tmp->next = head;                               // link to current list
        head = tmp;                                     // new head of list
    }
    fclose(fp);
    //free (bufor);                                     // can't free this
    //fclose(fp);                                       // close once only
    return head;                                        // return head of list
}

【讨论】:

  • 如果 bufor 会得到 x>100 字符串怎么办?如何防止呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-15
  • 1970-01-01
  • 2018-04-05
  • 2014-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多