【问题标题】:Read from a text file using link list使用链接列表从文本文件中读取
【发布时间】:2019-10-28 06:04:29
【问题描述】:

尝试使用此代码读取文本文件。为什么什么都不显示?

#include<stdio.h>
#include<stdlib.h>
#define SIZE 100
typedef struct head{
    int total;
    char quest[SIZE],quest1[SIZE],quest2[SIZE],quest3[SIZE];
    struct head *next;
    }LIST;

int main(){
    LIST *ch;
    FILE *fp=fopen("File.txt","r");
    if(fp==NULL){
        printf("File doesn't exist");
        exit(0);
    }
    else{
        fscanf(fp,"%s",&ch->total);
        fscanf(fp,"%s",&ch->quest);
        fscanf(fp,"%s",&ch->quest1);
        fscanf(fp,"%s",&ch->quest2);
        fscanf(fp,"%s",&ch->quest3);
    }
    printf("%s",ch->total);
    printf("%s",ch->quest);
    printf("%s",ch->quest1);
    printf("%s",ch->quest2);
    printf("%s",ch->quest3);
    fclose(fp);
    return 0;
    }

输出:

4
Programming
Stack
Linked List
Pointer Structure

任何帮助将不胜感激,谢谢。 .

【问题讨论】:

  • 请至少提供文件“File,txt”的第一行
  • fscanf(fp,"%s",&amp;ch-&gt;total); 是错误的——你需要%d(并且printf("%s",ch-&gt;total); 也需要%d)。那么fscanf(fp,"%s",&amp;ch-&gt;quest); 也是错误的;您不需要 &amp; — 其他字符串也是如此。您应该检查每个fscanf() 调用以确保它成功。
  • 第一行是 - 4 第二行 - 编程第 3 行 - 堆栈第 4 行 - 链表最后一行 - 指针结构
  • edit 并使用stackoverflow.com/editing-help 以更有用的方式提供文本文件的内容。
  • 请注意,scanf()%s 格式会读取一个“单词”——因此您将得到 LinkedList 读入 quest2quest3。我已经告诉过你如何解决我发现的问题。其中一个答案(我键入的唯一一个)确定了另一个问题(没有为ch 分配内存指向)。您的打印格式会将所有数据一起运行,因为格式中没有空格,您应该以换行符结束输出。

标签: c linked-list file-handling


【解决方案1】:

问题代码有一个小缺陷。

int main(){
    LIST *ch;

变量 ch 是一个指针,但它指向的位置是个问题。基本上,它可能指向进程可用的任何内存位置。设置这个指针很重要,以便它指向一个合适的内存位置。例如,malloc() 函数可用于分配合适的内存位置。例如:

int main(){
    LIST *ch = malloc(sizeof(*ch));

这是问题代码的另一个版本,可能效果更好:

文件.txt:

  4
  Programming
  Stack
  Linked_List
  Pointer_Structure

新代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>

    #define SIZE 100

    typedef struct head{
      int total;
      char quest[SIZE],quest1[SIZE],quest2[SIZE],quest3[SIZE];
      struct head *next;
      }LIST;

    int main()
      {
      int rCode=EXIT_SUCCESS;
      LIST *ch = NULL;

      FILE *fp=fopen("File.txt","r");
      if(!fp)
        {
        rCode=errno;
        fprintf(stderr, "fopen() failed.  errno[%d]\n", rCode);
        goto CLEANUP;
        }

      ch = malloc(sizeof(*ch));
      if(!ch)
        {
        rCode=errno;
        fprintf(stderr, "malloc() failed. errno[%d]\n", rCode);
        goto CLEANUP;
        }

      fscanf(fp,"%d", &ch->total); // %d, not %s, as per Jonathan Leffler
      fscanf(fp,"%s",ch->quest);   // Notice that &ch->quest was changed to ch->quest
      fscanf(fp,"%s",ch->quest1);  //   "      "     "        "     "     "    "
      fscanf(fp,"%s",ch->quest2);  //   "      "     "        "     "     "    "
      fscanf(fp,"%s",ch->quest3);  //   "      "     "        "     "     "    "

      printf("%d\n", ch->total); // %d, not %s, as per Jonathan Leffler
      printf("%s\n",ch->quest);
      printf("%s\n",ch->quest1);
      printf("%s\n",ch->quest2);
      printf("%s\n",ch->quest3);

    CLEANUP:

      if(ch)
        free(ch);

      if(fp)
        fclose(fp);

      return(rCode);
      }

输出:

  4
  Programming
  Stack
  Linked_List
  Pointer_Structure

【讨论】:

  • 你能用它来解释没有输出吗?
  • 感谢代码已经在运行,但仍然存在一些问题,输出未按我的预期显示。
  • errno有什么用?
  • @Angel,如果 fopen() 和 malloc() 等函数失败,则返回 NULL。为了纠正问题,了解失败的原因很重要;所以这些函数,当它们失败时,会在全局“errno”变量中放置一个代码来指示失败的原因。例如,如果调用了 malloc(),但没有可用内存,则 malloc() 将返回 NULL,然后将 errno 设置为 ENOMEM。 (参见 malloc() 的文档)
猜你喜欢
  • 2011-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 2014-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多