簡體   English   中英

我不知道如何將輸入文件中的字符串(單詞)讀入鏈表

[英]i cannot figure out how to read the strings(words) from an input file into a linked list

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

我的程序應該將每個單詞讀入鏈接列表,但我無法弄清楚如何從輸入文件中獲取每個單詞/字符串。 輸入文件是一個 ASCII 文本文件。 有什么建議? 謝謝你的幫助。

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

例如,您可以使用非常低級別的字符掃描,如下例所示 - 它可能會擴展為實際接受多個分隔符、分隔符重復等:

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

int main() {
  FILE *fptr;
  char buf[100];
  char *p, *s;

  strcpy(buf, "this is just a test");

  fptr=fopen("test.txt","r");
  while(fgets(buf, sizeof(buf), fptr) != NULL)
  {
    printf("LINE: %s\n", buf);
    /* scan characters and print tokens - single space is a separator */
    p = s = buf;
    while(*p!=0) {
      if (*p==' ') {
        *p = 0;
        printf("TOKEN: %s\n", s);
        s = p+1;
      }
      p++;
    }
    printf("TOKEN: %s\n", s);

  }


  fclose(fptr);
  return 0;
}

調用可能如下所示:

$ cat test.txt
this is test1
this is test2
$ gcc tokens.c && ./a.out
LINE: this is test1

TOKEN: this
TOKEN: is
TOKEN: test1

LINE: this is test2

TOKEN: this
TOKEN: is
TOKEN: test2
#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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM