简体   繁体   English

使用链接列表从文本文件中读取

[英]Read from a text file using link list

Trying to read a text file using this codes.尝试使用此代码读取文本文件。 Why doesn't it display anything?为什么什么都不显示?

#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;
    }

Output: Output:

4
Programming
Stack
Linked List
Pointer Structure

any help would be greatly appreciated thank you.任何帮助将不胜感激,谢谢。 . .

There is a small flaw in the question code.问题代码有一个小缺陷。

int main(){
    LIST *ch;

The variable ch is a pointer, but where it points is a problem.变量 ch 是一个指针,但它指向的位置是个问题。 Basically, it could be pointing to any memory location available to the process.基本上,它可能指向进程可用的任何 memory 位置。 It is important to set this pointer, so that it points to a suitable memory location.设置此指针很重要,以便它指向合适的 memory 位置。 For example, the malloc() function could be used to allocate a suitable memory location.例如,malloc() function 可用于分配合适的 memory 位置。 For example:例如:

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

Here is a an alternate version of the question code that might work better:这是问题代码的替代版本,可能会更好:

file.txt:文件.txt:

  4
  Programming
  Stack
  Linked_List
  Pointer_Structure

New code:新代码:

    #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);
      }

Output: Output:

  4
  Programming
  Stack
  Linked_List
  Pointer_Structure

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM