我无法弄清楚如何读取输入文件中的字符串(字)到一个链表 [英] i cannot figure out how to read the strings(words) from an input file into a linked list

查看:272
本文介绍了我无法弄清楚如何读取输入文件中的字符串(字)到一个链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
结构节点
{
  字符*的数据;
  结构节点*接下来的;
};
无效insertNode(结构节点**,字符*);
无效的printList(结构节点*);
诠释的main()
{
  结构节点*头= NULL;
  FILE * FPTR;
  焦炭FILE_NAME [20];
  焦炭海峡[1000];
  INT numOfChar;
  输出(输入文件的名称:);
  scanf函数(%S,FILE_NAME);
  的printf(请输入每行的字符数:);
  scanf函数(%d个,&安培; numOfChar);
  FPTR = FOPEN(FILE_NAME,R);
    炭令牌[100];
  而(与fgets(海峡,sizeof的(STR),FPTR)!= NULL)
  {    而(sscanf的(STR,%S,代币)!= EOF)
    {    }
  }
  FCLOSE(FPTR);
  的printList(头);
  返回0;
}
无效insertNode(结构节点** nodeHead,字符*数据)
{
    结构节点* new_node =(结构节点*)malloc的(的sizeof(结构节点));
    结构节点*最后= * nodeHead;
    字符*海峡;
    海峡=(字符*)malloc的(60 * sizeof的(炭));
    的strcpy(STR,数据);
    new_node->数据= STR;
    new_node->接着= NULL;
    如果(* nodeHead == NULL)
    {
       * nodeHead = new_node;
       返回;
    }
    而(与最后所GT;!下次= NULL)
    {
        最后的最后所=>接下来,
    }
    最后所>接下来= new_node;
}

我的程序应该是在每一个字读成一个链表,但我无法弄清楚如何让每一个字/串出的输入文件。输入文件是一个ASCII文本文件。有什么建议么?谢谢你的帮助。

 无效的printList(结构节点*节点)
{
    而(节点!= NULL)
    {
        的printf(%S,与于节点GT;数据);
        节点=于节点>接下来,
    }
}


解决方案

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
结构节点
{
  字符*的数据;
  结构节点*接下来的;
};
无效insertNode(结构节点**,字符*);
无效的printList(结构节点*);
诠释的main()
{
  结构节点*头= NULL;
  FILE * FPTR;
  焦炭FILE_NAME [20];
  焦炭海峡[1000];
  INT numOfChar;
  输出(输入文件的名称:);
  scanf函数(%S,FILE_NAME);
  的printf(请输入每行的字符数:);
  scanf函数(%d个,&安培; numOfChar);
  FPTR = FOPEN(FILE_NAME,R);  而(的fscanf(FPTR,%S,STR)!= EOF)
  {
      insertNode(安培;头,STR);  }  FCLOSE(FPTR);
  的printList(头);
  返回0;
}
无效insertNode(结构节点** nodeHead,字符*数据)
{
    结构节点* new_node =的malloc(sizeof的* new_node);
    new_node->数据=的strdup(数据);
    new_node->接着= NULL;    而(* nodeHead)
        nodeHead =及(* nodeHead) - >接着,
    * nodeHead = new_node;
}无效的printList(结构节点*节点)
{
    而(节点!= NULL)
    {
        的printf(%S,与于节点GT;数据);
        节点=于节点>接下来,
    }
}

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


struct node
{
  char *data;
  struct node *next;
};


void insertNode(struct node**, char *);
void printList(struct node*);


int main()
{
  struct node *head = NULL;
  FILE *fptr;
  char file_name[20];
  char str[1000];
  int numOfChar;


  printf("Enter the name of the file: ");
  scanf("%s",file_name);


  printf("Enter the number of characters per line: ");
  scanf("%d",&numOfChar);


  fptr=fopen(file_name,"r");
    char tokens[100];
  while(fgets(str, sizeof(str), fptr) != NULL)
  {

    while (sscanf(str, "%s", tokens) != EOF)
    {

    }


  }


  fclose(fptr);
  printList(head);


  return 0;
}


void insertNode(struct node** nodeHead, char *data)
{
    struct node* new_node = (struct node*) malloc(sizeof(struct node));
    struct node *last = *nodeHead;
    char *str;


    str= (char *)malloc(60*sizeof(char));
    strcpy(str, data);


    new_node->data  = str;
    new_node->next = NULL;


    if (*nodeHead == NULL)
    {
       *nodeHead = new_node;
       return;
    }


    while (last->next != NULL)
    {
        last = last->next;
    }


    last->next = new_node;
}

My program is supposed to read in each word into a linked list, but I cannot figure out how to get each word/string out of the input file. The input file is an ASCII text file. Any suggestions? Thanks for your help.

void printList(struct node* node)
{
    while(node != NULL)
    {
        printf(" %s ", node->data);
        node = node->next;
    }
}

解决方案

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


struct node
{
  char *data;
  struct node *next;
};


void insertNode(struct node**, char *);
void printList(struct node*);


int main()
{
  struct node *head = NULL;
  FILE *fptr;
  char file_name[20];
  char str[1000];
  int numOfChar;


  printf("Enter the name of the file: ");
  scanf("%s",file_name);


  printf("Enter the number of characters per line: ");
  scanf("%d",&numOfChar);


  fptr=fopen(file_name,"r");

  while(fscanf(fptr, "%s ", str) != EOF)
  {
      insertNode(&head, str);

  }



  fclose(fptr);
  printList(head);


  return 0;
}


void insertNode(struct node** nodeHead, char *data)
{
    struct node* new_node = malloc(sizeof *new_node);
    new_node->data = strdup(data);
    new_node->next = NULL;

    while (*nodeHead)
        nodeHead = &(*nodeHead)->next;
    *nodeHead = new_node;
}

void printList(struct node* node)
{
    while(node != NULL)
    {
        printf(" %s ", node->data);
        node = node->next;
    }
}

这篇关于我无法弄清楚如何读取输入文件中的字符串(字)到一个链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆