Zapisywanie listy jednokierunkowej w C

0

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);
}
1

Jeżeli chcesz zapisać dane w postaci tekstowej (czyli takiej, która będzie czytelna dla człowieka) to:

void save(ListElement_type *head)
{
    FILE *plik = fopen("C:\\Documents and Settings\\Patryk1\\Pulpit\\1.txt","w+");
    
    if (plik == NULL)
    {
        printf("Błąd zapisu");
        return;
    }

    while(head)
    {
        fprintf(plik, "%d\n", head->data);
        head = head->next;
    }

    fclose(plik);
}

Nie kompilowałem ale powinno działać.

0

takie coś napisałem

{
    FILE *plik;
    plik=fopen("C:\\Documents and Settings\\Patryk1\\Pulpit\\1.txt","r");
    while(plik){
        fscanf(plik,"%d",head->data);
        head=head->next;
    }
    fclose(plik);
}
0

Spróbuj coś takiego. Niestety ponownie nie kompilowałem i nie sprawdzałem czy działa.

void read(ListElement_type **head)
{
    FILE *plik;
    int number;
    plik=fopen("C:\\Documents and Settings\\Patryk1\\Pulpit\\1.txt","r");

    while(fscanf(plik, "%d", &number) != EOF){
        // Wartosc zmiennej number wstaw do listy.
    }
    fclose(plik);
}

Liczbę powinieneś mieć w zmiennej number - później możesz tę liczbę wstawić do listy wzorując się na kodzie, ktory już masz czyli funkcji push_back

0

Zapisywanie wskaźników z nadzieją na operację odwrotną jest bez sensu. Mogę sobie jedynie wyobrazić zapis wskaźnika do pliku na etapie debugowania

1 użytkowników online, w tym zalogowanych: 0, gości: 1