Witam! Mam napisaną część programu (niesamodzielnie) - dodawanie nowych rekordów do listy, usunięcie danego rekordu oraz wyświetlenie wszystkich aktualnie istniejących rekordów. Interesuje mnie mianowicie zapis do pliku oraz odczyt z pliku ponieważ prezentowany kod ma być małą bazą danych. Przepraszam za przejrzystość kodu ale jest w trakcie sporych poprawek. Z góry dzięki za pomoc i proszę o wyrozumiałość!
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <iomanip>
using namespace std;
struct student
{
int indeks;
char imie[20];
char nazwisko[20];
student *next;
};
student *dodaj_studenta(student *lista)
{
student *wsk;
wsk=lista;
student *dane=new (student);
dane->next=NULL;
printf(" Prosze podac numer indeksu studenta : ");
scanf("%d", &dane->indeks);
printf(" Prosze podac nazwisko studenta : ");
scanf("%s", &dane->imie);
printf(" Prosze podac imie studenta : ");
scanf("%s", &dane->nazwisko);
if (lista==NULL) lista=dane;
else
{
while (wsk->next!=NULL)
wsk=wsk->next;
wsk->next=dane;
}
return lista;
};
void wyswietl (student *lista)
{
if (lista==NULL)
{
printf(" Brak studentow w bazie !\n");
printf("\n");
}
else do
{
cout<<setw(5)<<lista->indeks;
cout<<setw(20)<<lista->imie;
cout<<setw(30)<<lista->nazwisko<<endl;
if (lista!=NULL) lista=lista->next;
}
while (lista!=NULL);
};
student *usun (student *lista)
{
student *temp,*wsk;
wsk=lista;
if (lista==NULL)
{
printf(" Brak studentow w bazie !\n");
printf("\n");
system("pause");
system("CLS");
}
else
{
int indeks;
printf(" Podaj indeks studenta\n");
scanf("%d",&indeks);
getchar();
printf("%d",indeks);
if (wsk->indeks==indeks)
{
temp=wsk;
wsk=wsk->next;
free (temp);
}
else
{
while (wsk->next!=NULL&&wsk->next->indeks!=indeks)
wsk=wsk->next;
if (wsk->next==NULL) printf(" Nie ma takiego studenta w bazie\n");
else
{
temp=wsk->next;
wsk->next=wsk->next->next;
free(temp);
}
}
}
return lista;
};
int main()
{
int znak;
student *listastudentow=NULL;
do
{
printf("1. Dodaj studenta.\n");
printf("2. Usun studenta (numer indesku).\n");
printf("3. Wyswietl liste studentow.\n");
printf("4. Sortowanie listy studentow.\n");
printf("5. Zapisz liste studentow.\n");
printf("6. Wczytaj liste studentow.\n");
printf("7. Koniec programu.\n");
printf("\n");
printf(" Twoj wybor : ");
scanf("%d",&znak);
if((znak!=1)&&(znak!=2)&&(znak!=3)&&(znak!=4)&&(znak!=5)&&(znak!=6)&&(znak!=7))
{
printf("\n");
printf(" Nie ma takiej opcji ! Podaj jeszcze raz !\n\n");
system("pause");
system("CLS");
}
else
{
switch(znak)
{
case 1:
{
printf("\n");
listastudentow = dodaj_studenta(listastudentow);
printf("\n");
printf(" Dodanie studenta zakonczylo sie sukcesem !\n\n");
system("pause");
system("cls");
break;
}
case 2:
{
listastudentow=usun(listastudentow);
break;
}
case 3:
{
wyswietl(listastudentow);
system("pause");
system("CLS");
break;
}
case 4:
{
break;
}
case 5:
{
break;
}
case 7:
{
printf("\n");
printf(" Koniec programu ! Dowidzenia !\n\n");
system("pause");
return 0;
}
}
}
}
while(znak != 7);
}