Tylko dalej jest problem z tymi krzaczkami, kiedy wywalisz wszystkie pozycje :-/, poprawiłbym to, tylko te źródła są strasznie brudne... Ja bym to wolał od nowa napisać.... Ale i tak jest chyba lepiej niż było.
#include<condefs.h>
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<io.h>
struct car
{
char marka[10];
char model[10];
char kolor[10];
char produkcja[20];
int rocznik;
int cena;
};
void dopisz(int *nr); // F5
void usun(char *s, int *p_file_size); // F6
void modyfikuj(void); // F7
void sortuj(int *); // F8
void drukuj_rekord();
void drukuj_p();
int menu(void);
int edycja(void);
void status(char *s);
void zmien_plik(void);
FILE *fp;
char s[100]; // nazwa pliku
int main(int argc, char **argv)
{
fprintf(stdout,"Podaj sciezke do pliku: ");
fscanf(stdin, "%s",s);
/* uznałem tą cześć kodu za wybitnie ni potrzebna...
char ch;
struct car tab[9] = {
{ "BMW", "C1", "czarny", "niemiecka", 1999, 220000 },
{ "BMW", "C5", "zielony", "niemiecka", 1996, 300000 },
{ "BMW", "X5", "czarny", "niemiecka", 2002, 550000 },
{ "Peugeot", "206", "srebrny", "francuska", 2002, 134000 },
{ "Peugeot", "307", "zielony", "francuska", 2003, 150000 },
{ "Peugeot", "407", "czarny", "francuska", 2001, 134000 },
{ "Peugeot", "Partner", "metalik", "francuska", 2003, 150000 },
{ "Fiat", "Seicento", "bialy", "polska", 1995, 12000 },
{ "Fiat", "Bravo", "czerwony", "polska", 1997, 45000 },
};
struct car tab1[22];
// TODO: nazwa pliku
*/
//to dal sprawdzenia czy plik isntnieje, metoda tak sobie ale działa...:
bool nowy_plik = 0;
fp = fopen(s, "r+");
if(fp==NULL)
nowy_plik = 1;
fclose(fp);
fp = fopen(s, "a+b");
// fwrite(&tab, sizeof(car), 22, fp);
if(nowy_plik)
{
struct car st;
strcpy(st.marka, "Fiat");
strcpy(st.model, "Cinquecento");
strcpy(st.kolor, "Czerwony");
strcpy(st.produkcja, "ZSRR :P");
st.rocznik = 1978;
st.cena = 500000;
fwrite(&st, sizeof(car), 1, fp);
}
while(1)
menu();
fclose(fp);
return 0;
}
int menu(void)
{
char ch, size;
window(1, 1, 80, 25);
clrscr();
gotoxy(10, 10);
printf(" 1. Zmien plik ");
gotoxy(10, 11);
printf(" 2. Otworz plik ");
gotoxy(10, 12);
printf(" 3. Zakoncz ");
fflush(stdin);
do
{
ch = getch();
}
while (ch != '1' && ch != '2' && ch != '3' && ch != 27);
switch(ch)
{
case '1' : {zmien_plik(); break;}
case '2' : {edycja(); break;}
case 27 : case '3' : {exit(0);}
}
}
void zmien_plik(void)
{
clrscr();
fprintf(stdout,"Podaj sciezke do pliku: ");
fscanf(stdin, "%s",s);
fp = fopen(s, "a+b");
clrscr();
while(1)
menu();
fclose(fp);
exit (0);
}
int edycja(void)
{
int i, size, file_size;
int ch1, ch2, nr;
struct car st;
size = sizeof(struct car);
fseek(fp, 0L, SEEK_END);
file_size = ftell(fp) / size;
fseek(fp, 0L, SEEK_SET);
fread(&st, sizeof(car), 1, fp);
clrscr();
gotoxy(1, 1);
printf(" Przeglad... F5 - Dopisz , F6 - Usun, F7 - Modyfikuj, F8 - Sortuj, Esc - menu ");
gotoxy(1, 25);
status("PRZEGLAD PgDn PgUp \030 \031 Home End ");
window(1, 10, 40, 24);
printf("%d %s %s %s %s %i %i", 1, st.marka, st.model, st.kolor, st.produkcja, st.rocznik, st.cena);
do
{
ch1 = getch();
ch2 = 0;
if (ch1 == 0) ch2 = getch();
if (ch1 == 27) ch2 = 27;
if ((ch1 >= 49) && (ch1 <= 60)) ch2 = ch1;
switch(ch2)
{
case 72 : case 73 : case 56 : case 57 : // PgUp lub strzalka w gore
if ((ftell(fp) - size) > 0)
fseek(fp, -2L*size, SEEK_CUR);
else
fseek(fp, 0L, SEEK_SET);
drukuj_rekord();
break;
case 80 : case 81 : case 50 : case 51 : // PgDn lub strzalka w dol
if (ftell(fp) / size < file_size)
drukuj_rekord();
break;
case 71 : case 55: // Home, poczatek
fseek(fp, 0L, SEEK_SET);
drukuj_rekord();
break;
case 79 : case 49 : // End, koniec
fseek(fp, -1L*size, SEEK_END);
drukuj_rekord();
break;
case 27 :
return 0;
break;
case 63 : // F5
dopisz(&file_size);
drukuj_p();
break;
case 64 : // F6
usun(s, &file_size);
drukuj_p();
break;
case 65 : // F7
modyfikuj();
drukuj_p();
break;
case 66 : // F8
sortuj(&file_size);
drukuj_p();
}
}
while (1 < 2);
}
void dopisz(int *p_nr) // F5
{
status(" DOPISZ ");
char ch;
fseek(fp, 0L, SEEK_END);
int nr_rek = ftell(fp) / sizeof(car);
int i = 0;
struct car st;
while(1)
{
clrscr();
fflush(stdin);
printf("\n Rekord nr %d ", ++nr_rek);
printf("\n Marka: ");
scanf("%s", st.marka);
fflush(stdin);
printf("\n, Model: ");
scanf("%s", st.model);
fflush(stdin); // TODO !
printf("\n, Kolor: ");
scanf("%s", st.kolor);
fflush(stdin); // TODO !
printf("\n, Produkcja: ");
scanf("%s", st.produkcja);
fflush(stdin); // TODO !
printf("\n, Rocznik: ");
scanf("%i", &st.rocznik);
fflush(stdin); // TODO !
printf("\n, Cena: ");
scanf("%i", &st.cena);
fwrite(&st, sizeof(car), 1, fp);
*p_nr = nr_rek;
printf("\n Nastepny rekord? [T/N]");
fflush(stdin);
ch = toupper(getc(stdin));
if (ch == 'N') break;
}
clrscr();
status("PRZEGLAD");
fflush(stdin);
}
void usun(char *s, int *p_file_size) // F6
{
status("USUN ");
int handle;
int nr_rek, ilosc;
struct car st;
clrscr();
nr_rek = ftell(fp) / sizeof(struct car);
// printf("\n nazwisko: %s", st.nazwisko);
fseek(fp, -1L*sizeof(car), SEEK_END);
fread(&st, sizeof(car), 1, fp);
// printf("\n nazwisko: %s", st.nazwisko);
// getch();
fseek(fp, (nr_rek - 1)*sizeof(car), SEEK_SET);
fwrite(&st, sizeof(car), 1, fp);
fseek(fp, 0L, SEEK_END);
ilosc = ftell(fp) / sizeof(struct car);
fclose(fp);
if ((handle = open(s, O_RDWR|O_BINARY)) == -1)
printf("\n Plik nie zostal otwarty");
chsize(handle, (ilosc - 1)*sizeof(car));
close(handle);
fp = fopen(s, "r+b");
fseek(fp, 0L, SEEK_END);
*p_file_size = ftell(fp) / sizeof(struct car);
fseek(fp, 0L, SEEK_SET);
status("PRZEGLAD");
fflush(stdin);
}
void modyfikuj(void) // F7
{
clrscr();
printf("\ modyfikuj ");
fseek(fp, 0L, SEEK_SET);
getch();
fflush(stdin);
}
void sortuj(int *p_nr) // F8
{
int i, zam;
struct car st1, st2;
status("SORTUJ");
do
{
zam = 0;
fseek(fp, 0L, SEEK_SET);
for (i = 0; i < *p_nr - 1; i++)
{
fseek(fp, long(i*sizeof(car)), SEEK_SET);
fread(&st1, sizeof(car), 1, fp);
fread(&st2, sizeof(car), 1, fp);
if (strncmp(st1.marka, st2.marka, 18) > 0)
{
fseek(fp, long(i*sizeof(car)), SEEK_SET);
fwrite(&st2, sizeof(car), 1, fp);
fwrite(&st1, sizeof(car), 1, fp);
zam = 1;
}
}
}
while (zam);
clrscr();
fseek(fp, 0L, SEEK_SET);
status("PRZEDLAD");
// getch();
fflush(stdin);
}
void drukuj_rekord()
{
int nr_rek, size;
struct car st;
size = sizeof(struct car);
fread(&st, sizeof(car), 1, fp);
nr_rek = ftell(fp) / size;
clrscr();
//mozna by najpierwej wyczyscic, bo zostana smieci...
gotoxy(1, 25);
window(1, 10, 40, 24);
printf(" %d ", nr_rek);
printf(" %s ", st.marka);
printf(" %s ", st.model);
printf(" %s ", st.kolor);
printf(" %s ", st.produkcja);
printf(" %i ", st.rocznik);
printf(" %i ", st.cena);
//najgłupsza, ale działająca metoda, na to, by nie zostawały śmieci...
printf(" ");
}
void drukuj_p(void)
{
struct car st;
clrscr();
fseek(fp, 0L, SEEK_SET);
fread(&st, sizeof(car), 1, fp);
printf("%s %s %s %s %i %i", st.marka, st.model, st.kolor, st.produkcja, st.rocznik, st.cena);
}
void status(char *s)
{
window(1, 1, 40, 25);
gotoxy(1, 25);
printf("Status: %s", s);
window(1, 10, 40, 24);
}
PS> Sorry, że tak długo, ale miałem zasadniczą przerwę w pracy :)