Found this code somewhere, (didn't know bumping wasn't allowed so I post it here) it should do what I want to do.
Code:
struct list{
char text[255];
int num;
struct list *next;
struct list *prior;
} list_entry;
//code on reading inputing
printf("Enter file content (CTRL-Z to end) : \n");
while(gets(s)){
info = (struct list *)malloc(sizeof(list_entry));
if(!info){
printf("\nnot enough memory");
return;
}
strcpy(info->text,s);
printf("info : %s",info);
listStore(info, &start, &last);
}
printf("\n");
store();
//code on writing to a file
if((cfPtr=fopen(file,"w"))==NULL)
printf("File could not be opened\n");
else{
while(info){
printf("info2:%s",info->text);
fwrite(info,sizeof(struct list),1,cfPtr);
fflush(cfPtr);
info = info->next;
}
fclose(cfPtr);
info = start;
clearList(&info);
printf("File saved\n");
}
//methods
void listStore(struct list *i, struct list **start, struct list **last)
{
struct list *p;
p = *last;
if(*last ==NULL){ //first element in list
i->next = NULL;
i->prior = NULL;
i->num = 0;
*last = i;
*start = i;
return;
}
else{
p->next = i;
i->next = NULL;
i->prior = p;
i->num = (p->num)+1;
*last = i;
}
}
//to view
clearList(&info);
printf("\nLoading File...\n");
system("cls");
int count=0;
while(!feof(fp)){
info = (struct list *)malloc(sizeof(list_entry));
if(!info){
printf("\nnot enough memory");
return 0;
}
if(1!=fread(info, sizeof(struct list), 1, fp)break;
printf("info3 :%s",info);
listStore(info,&start,&last);
num++;
count++;
}
fclose(fp);
Only prob is what types do the variables need? Hope someone can help me out.