OK, Thanks guys! :-)
Considering aforesaid i have written this code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSTRLEN 4096
#define SOMEFILE "somefile.txt"
typedef struct list list;
struct list
{
char *str;
list *next;
};
list *item_new(char *str)
{
list *lp;
if (str == NULL)
return NULL;
lp = (list *) malloc(sizeof(list));
if (lp == NULL)
return NULL;
lp->str = str;
lp->next = NULL;
return lp;
}
list *item_add(list *lp, list *np)
{
np->next = lp;
return np;
}
void free_list(list *lp)
{
list *next;
for ( ; lp != NULL; lp = next) {
next = lp->next;
free(lp->str);
free(lp);
}
}
int main(void)
{
FILE *fp;
list *lp;
char *tp, buf[MAXSTRLEN];
lp = NULL;
fp = fopen(SOMEFILE, "r");
if (fp == NULL)
exit(1);
while (fgets(buf, MAXSTRLEN, fp) != NULL) {
tp = strdup(buf);
if (tp == NULL)
exit(1);
lp = item_add(lp, item_new(tp));
}
fclose(fp);
for ( ; lp != NULL; lp = lp->next)
printf ("%s", lp->str);
free_list(lp);
return 0;
}
Works fine, but i not certain that has done all correct, could not you correct me if i have allowed here blunders?
Thank you!