我想创建一个链表,其中包含来自input.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 %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;
}
我从未与strcat合作,以某种方式不起作用。我也尝试使用char数组而不是指针,但是结果相同。 也许我必须使用其他功能?
以某种方式,输出是奇怪的,似乎它从未在add()函数的if块中进行。 在.txt文件中输出2个名称: pt? pt? 彼得·帕克 克拉克·肯特
更改了add()函数的Return类型,现在可以使用了:)
答案 0 :(得分:0)
strcat
绝对不是正确的选择。正如您的编译器应该告诉您的那样,您首先要向其中传递错误类型的变量。它需要2个字符串,character
只是一个char
。
C中的字符串是NUL终止的,并且刚刚为anme
和fnme
分配了内存,它们都不会被初始化,因此不会包含该NUL终止,因此您将遇到未定义的行为。
相反,只是将它们当作数组对待,并在读取字符时存储字符,然后记住在完成所有字符的读取后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
此方法还允许您检查count
是否超出分配的大小,在这种情况下为99,因为NUL字符需要最后一个空格。
答案 1 :(得分:0)
稍微修改一下代码,它应该可以工作。您不能以这种方式使用strcat
,因为strcat
的第二个参数接受const char *
,并且您拥有char
,因此您的代码将无法编译。
这里值得一提的是:我假设函数add
将为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);
}
答案 2 :(得分:0)
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 %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;
}