【发布时间】:2010-09-17 11:48:44
【问题描述】:
我正在尝试制作一个程序,逐行读取文件,然后将读取的行放入链接列表中,我的问题是将字符串添加到列表中。看代码,在else测试中可以看到我的问题。
#include<stdlib.h>
#include<stdio.h>
struct list_el {
char *ord;
struct list_el * next;
};
typedef struct list_el item;
int main(int argc, char *argv[]) {
int c;
item *curr, *head;
head = NULL;
FILE *fileHandle = fopen("tresmaa.txt", "r");
while((c = fgetc(fileHandle)) != '\n' || c != EOF)
if(c == EOF) {
printf("\n");
break;
} else {
curr = (item*)malloc(sizeof(item));
curr->ord = "I cant point curr -< ord = c, how can i point the readed sentences to the value Ord?";
curr->next = head;
head = curr;
putchar(c);
}
curr = head;
while(curr) {
printf("%s\n", curr->ord);
curr = curr->next ;
}
}
【问题讨论】:
-
除了你的问题,这段代码是内存泄漏!您需要
free使用malloc分配的所有内存。 -
谢谢。我将 free(curr) 添加到我的代码中:D
标签: c linked-list cstring