简体   繁体   English

从.txt文件中读取名称,并将其添加到链接列表中

[英]Read names from .txt File and add it to a linked list

I want to create a linked list, wich contains names from a input.txt File. 我想创建一个链表,其中包含来自input.txt文件的名称。 First name and last name are seperatet by a blank space and after the last name there is a linebreak. 名字和姓氏由空格分隔,姓氏后有换行符。

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

typedef struct node{
   char* firstname;
   char* lastname;
   struct node *next;
   }node;

node *add(node *head, char* fnme, char* lnme){
   node *new_node;
   new_node = (node*)malloc(sizeof(node));
   if(new_node == NULL)
      printf("Fehler bei Speicher reservierung...");
   new_node->firstname = (char*)malloc(100*sizeof(char));
   if(new_node->firstname == NULL)
      printf("Fehler bei Speicher reservierung...");
   new_node->lastname = (char*)malloc(100*sizeof(char));
   if(new_node->lastname == NULL)
      printf("Fehler bei Speicher reservierung...");

   strcpy(new_node->firstname, fnme);
   strcpy(new_node->lastname, lnme);

   if(head == NULL){
      head = new_node;
      head->next = NULL;
      return head;
   }

   node *current;
   current = head;

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

   current->next = new_node;
   new_node->next = NULL;
   return head;
 }

 void print(node *head){
   node *current;
   current = head;

   while(current != NULL){
     printf("%s %s\n", current->firstname, current->lastname);
     current = current->next;
    }
   }

int main(){

  node *head = NULL;

  char character;

  FILE *fp;
  fp = fopen("input.txt", "r");

  while ((character = fgetc(fp)) != EOF) {
  char *fnme, *lnme;
  fnme = (char*)malloc(100 * sizeof(char));
  if(fnme == NULL)
     printf("Fehler bei Speicher reservierung...");
  lnme = (char*)malloc(100 * sizeof(char));
  if(lnme == NULL)
     printf("Fehler bei Speicher reservierung...");

  int i = 0;
  while (character != ' ') {
     fnme[i++] = character;
     character = fgetc(fp);
  }
  fnme[++i] = '\0';   // NULL-terminate

  i = 0;
  while (character != '\n') {
     lnme[i++] = character;
     character = fgetc(fp);
  }
  lnme[++i] = '\0';  // NULL-terminate

  head = add(head, fnme, lnme);   

  free(fnme);
  free(lnme);
 }
 print(head);
 return 0;
}

I never worked with strcat, somehow that doesnt work. 我从未与strcat合作,以某种方式行不通。 I also tried using char-arrays instead of pointers but its the same result. 我也尝试使用char数组而不是指针,但是结果相同。 Maybe I have to use other functions? 也许我必须使用其他功能?

Update 1: 更新1:

Somehow the Output is strange, seems like it's never going in the if-block in the add() function. 以某种方式,输出是奇怪的,似乎它从未在add()函数的if块中进行。 Output with 2 names in the .txt-file: pt? 在.txt文件中输出2个名称:pt? pt? PT? Peter Parker Klark Kent 彼得·帕克·克拉克·肯特

Update 2: 更新2:

Changed the Return type of the add() function, now it works :) 更改了add()函数的Return类型,现在可以使用了:)

strcat is definitely not the right thing to use. strcat绝对不是正确的选择。 As your compiler should be telling, you're firstly passing in the wrong type of variable in to it. 正如您的编译器应该告诉您的那样,您首先要向其中传递错误类型的变量。 It takes 2 strings and character is just a char . 它需要2个字符串,而character只是一个char

Strings in C are NUL terminated and having just allocated memory for anme and fnme , neither of them will be initialised so won't contain that NUL termination, so you'll be experiencing undefined behaviour. C中的字符串是NUL终止的,并且刚刚为anmefnme分配了内存,它们都不会被初始化,因此不会包含该NUL终止,因此您将遇到不确定的行为。

Instead, just treat them like an array and store the characters as you read them in and then remember to NUL terminate it when you've finished reading in all the characters. 相反,只要像对待数组一样对待它们,并在读入它们时存储字符,然后记住在读完所有字符后NUL将其终止。

int count = 0;

do{  
    fnme[count++] = character;
    character = fgetc(fp);
}while(character != ' ');
fnme[count]='\0'; // Need to NUL terminate the string

count = 0; // Remember to reset count to 0
do{  
    anme[count++] = character;
    character = fgetc(fp);
}while(character != '\n');
anme[count]='\0'; // Need to NUL terminate the string

This method also allows you to check that count doesn't exceed the size you've allocated, which in this case is 99 as the last space is needed for the NUL character. 此方法还允许您检查count是否不超过分配的大小,在这种情况下为99,因为NUL字符需要最后一个空格。

Modify your code a little bit, and it should work. 稍微修改您的代码,它应该可以工作。 You can not use strcat in that way, since the second parameter of strcat accepts const char * , and you have char , so your code won't compile. 您不能以这种方式使用strcat ,因为strcat的第二个参数接受const char * ,并且您拥有char ,因此您的代码将无法编译。

Also something noteworthy here is: I assume the function add will make a deepcopy for both fnme and anme , otherwise, you cannot simply free them here. 这里还有一点值得注意:我假设函数add将为fnmeanme都做一个fnme anme ,否则,您不能在这里简单地释放它们。

while ((character = fgetc(fp)) != EOF) {
    char *fnme, *anme;
    fnme = malloc(100 * sizeof(char));
    anme = malloc(100 * sizeof(char));

    int i = 0;
    while (character != ' ') {
        fnme[i++] = character;
        character = fgetc(fp);
    }
    fnme[++i] = '\0';   // NULL-terminate

    i = 0;
    while (character != '\n') {
        anme[i++] = character;
        character = fgetc(fp);
    }
    anme[++i] = '\0';  // NULL-terminate

    add(head, fnme, anme);   // Assume 'add' will make deep copy of both fname and 
                             // anme, otherwise you cannot free them here.
    free(fnme);
    free(anme);

}

You have some errors in add() function. 您在add()函数中add()一些错误。 This is the full solution. 这是完整的解决方案。 BTW, since this is a follow up question, you'd better ask it in a new post rather than updating the original one. 顺便说一句,由于这是一个后续问题,您最好在新帖子中提问,而不要更新原始帖子。

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

typedef struct node{
  char *forame;
  char *aftername;
  struct node *next;
}node;

void add(node **head, char* fnme, char* lnme){
  node *new_node;
  new_node = (node*)malloc(sizeof(node));
  new_node->forame = (char*)malloc(100*sizeof(char));
  new_node->aftername = (char*)malloc(100*sizeof(char));

  strcpy(new_node->forame, fnme);
  strcpy(new_node->aftername, lnme);

  if (*head == NULL){
    *head = new_node;
    new_node->next = NULL;
    return;
  }

  node *current;
  current = *head;

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

  current->next = new_node;
  new_node->next = NULL;
}

void print(node *head){
   node *current;
   current = head;

  while(current != NULL){
      printf("%s %s\n", current->forame, current->aftername);
      current = current->next;
  }
}

int main() {

  node *head = NULL;
  char character;

  FILE *fp;
  fp = fopen("input.txt", "r");

  while ((character = fgetc(fp)) != EOF) {
      char *fnme, *lnme;
      fnme = (char*)malloc(100 * sizeof(char));
      lnme = (char*)malloc(100 * sizeof(char));

      int i = 0;
      while (character != ' ') {
          fnme[i++] = character;
          character = fgetc(fp);
      }
      fnme[++i] = '\0';

      i = 0;
      while (character != '\n') {
          lnme[i++] = character;
          character = fgetc(fp);
      }
      lnme[++i] = '\0';

      add(&head, fnme, lnme);

     free(fnme);
     free(lnme);
  }

  print(head);
  return 0;
}

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

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