C语言 从文件中读取.txt名称并将其添加到链表中



我想创建一个链表,其中包含来自输入.txt文件的名称。 名字和姓氏用空格分隔,姓氏后面有一个换行符。

#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 %sn", 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] = '';   // NULL-terminate
i = 0;
while (character != 'n') {
lnme[i++] = character;
character = fgetc(fp);
}
lnme[++i] = '';  // NULL-terminate
head = add(head, fnme, lnme);   
free(fnme);
free(lnme);
}
print(head);
return 0;
}

我从来没有和strcat一起工作过,不知何故,这行不通。我也尝试使用字符数组而不是指针,但结果是一样的。 也许我必须使用其他功能?

更新 1:

不知何故,输出很奇怪,似乎它永远不会进入 add(( 函数中的 if 块。 输出在.txt文件中有 2 个名称:
pt?pt?
Peter Parker
Klark Kent

更新 2:

更改了 add(( 函数的返回类型,现在可以工作了

稍微修改一下你的代码,它应该可以工作。你不能以这种方式使用strcat,因为strcat的第二个参数接受const char *,并且你有char,所以你的代码不会编译。

这里还值得注意的是:我假设函数add将为fnmeanme制作深度副本,否则,您不能简单地在此处释放它们。

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] = '';   // NULL-terminate
i = 0;
while (character != 'n') {
anme[i++] = character;
character = fgetc(fp);
}
anme[++i] = '';  // 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);
}

strcat绝对不是正确的使用方式。正如编译器应该告诉的那样,您首先将错误类型的变量传递给它。它需要 2 根字符串,character只是一个char

C 中的字符串以 NUL 结尾,并且刚刚为anmefnme分配了内存,它们都不会被初始化,因此不会包含 NUL 终止,因此您将遇到未定义的行为。

相反,只需将它们视为一个数组,并在读取字符时存储字符,然后记住在完成所有字符的读取后终止它。

int count = 0;
do{  
fnme[count++] = character;
character = fgetc(fp);
}while(character != ' ');
fnme[count]=''; // 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]=''; // Need to NUL terminate the string

此方法还允许您检查count是否不超过您分配的大小,在本例中为 99,因为 NUL 字符需要最后一个空格。

add()函数中有一些错误。这是完整的解决方案。 顺便说一句,由于这是一个后续问题,您最好在新帖子中提出它,而不是更新原始帖子。

#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 %sn", 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] = '';
i = 0;
while (character != 'n') {
lnme[i++] = character;
character = fgetc(fp);
}
lnme[++i] = '';
add(&head, fnme, lnme);
free(fnme);
free(lnme);
}
print(head);
return 0;
}

相关内容

  • 没有找到相关文章

最新更新