Mam taki program który tworzy listę liczb, chciałbym teraz te liczby zapisać w pliku ale nie mam pojęcia jak to zrobić mógłby mi ktoś pomóc napisać funkcje save? Proszę :D
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
typedef struct ListElement {
int data;
struct ListElement * next;
} ListElement_type;
void show(ListElement_type *head);
int list_size(ListElement_type *head);
void push_back(ListElement_type **head, int number);
void pop_by_index(ListElement_type **head, int position);
void save(ListElement_type *head);
int main()
{
ListElement_type *head;
head = (ListElement_type *)malloc(sizeof(ListElement_type));
head=NULL;
int option=-1;
int number=-1;
int index=-1;
while(option!=0)
{
system("cls");
printf("\nAktualny stan listy: ");
show(head);
printf("\n\nDrogi uzytkowniku! Co chcesz zrobic?\n");
printf("1. Dodac element na koniec listy.\n");
printf("2. Usunac element o wybranym indeksie.\n");
printf("0. Zakonczyc program.\n");
scanf("%i", &option);
switch (option)
{
case 0:
return 0;
break;
case 1:
printf("Wpisz liczbe jaka chcesz dodac: ");
scanf("%i", &number);
push_back(&head, number);
break;
case 2:
printf("Wpisz indeks elementu, ktorego chcesz sie pozbyc raz na zawsze: ");
scanf("%i", &index);
pop_by_index(&head, index);
break;
case 3:
save(&head);
break;
default:
printf("Podaj wlasciwa opcje."); Sleep(2000);
break;
}
}
return 0;
}
void push_back(ListElement_type **head, int number)
{
if(*head==NULL)
{
*head = (ListElement_type *)malloc(sizeof(ListElement_type));
(*head)->data = number;
(*head)->next = NULL;
}else
{
ListElement_type *current=*head;
while (current->next != NULL) {
current = current->next;
}
current->next = (ListElement_type *)malloc(sizeof(ListElement_type));
current->next->data = number;
current->next->next = NULL;
}
}
void pop_by_index(ListElement_type **head, int position)
{
ListElement_type *current=*head;
ListElement_type *tmp;
int i=0;
while (current->next != NULL && i<position-1) {
current=current->next;
i++;
}
tmp = current->next;
current->next = tmp->next;
free(tmp);
}
void show(ListElement_type *head)
{
printf("\n");
if(head==NULL) printf("List is empty");
else
{
ListElement_type *current=head;
do {
printf("%i", current->data);
printf("\n");
current = current->next;
}while (current != NULL);//może current->next???
}
}
int list_size(ListElement_type *head)
{
int counter=0;
if(head==NULL) return counter;
else
{
ListElement_type *current=head;
do {
counter++;
current = current->next;
}while (current != NULL);
}
return counter;
}
void save(ListElement_type *head)
{
FILE *plik;
plik=fopen("C:\\Documents and Settings\\Patryk1\\Pulpit\\1.txt","w");
while(head){
fwrite(head,sizeof(struct ListElement),1,plik);
}
fclose(plik);
}