Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <fcntl.h> // file írás, olvasás
- #include <sys/types.h> //file
- #include <string.h>
- #include <time.h>
- #include <stdbool.h>
- #include <sys/stat.h>//időponthoz
- #include <unistd.h> //időponthoz
- typedef struct
- {
- char* megfigyelo_neve;
- char* telepules_nev;
- int nyuszi_db;
- char* megfigyelesi_ido;
- char* nyuszi_kor;
- } megfigyeles;
- static struct stat st;
- static int db = 0;
- static int *db_p = &db;
- static size_t memoria = 100;
- static size_t charhossz = 100;
- static char* filenev = "eredmeny.txt";
- void file_olvasas(megfigyeles* tomb, int* darab_p)
- {
- FILE *file= fopen(filenev, "r");
- *darab_p = 0;
- char output[512];
- while (fgets(output, 100, file) != NULL)
- {
- megfigyeles mf;
- sscanf (output, "%s\t %s\t %d\t %s\t %s\t",
- mf.megfigyelo_neve,
- mf.telepules_nev,
- &mf.nyuszi_db,
- mf.nyuszi_kor,
- mf.megfigyelesi_ido);
- tomb[*darab_p] = mf;
- ++(*darab_p);
- }
- fclose(file);
- }
- void file_iras(megfigyeles *mf){
- FILE *file;
- file = fopen(filenev, "a");
- fprintf(file, "%s\t %s\t %d\t %s\t %s\n",
- mf->megfigyelo_neve,
- mf->telepules_nev,
- mf->nyuszi_db,
- mf->nyuszi_kor,
- mf->megfigyelesi_ido);
- fclose(file);
- }
- void torles(megfigyeles *tomb)
- {
- int i;
- char *nev;
- char *tnev;
- nev = (char *)malloc(charhossz);
- tnev = (char *)malloc(charhossz);
- printf("Adja meg a modositani kivant adatokat!\n");
- printf("Adja meg a keresett nevet!\n");
- scanf("%s", nev);
- printf("Adja meg a keresett telepulest!\n");
- scanf("%s", tnev);
- megfigyeles *tomb2;
- tomb2 = (megfigyeles *)malloc(memoria);
- int j = 0;
- for(i = 0; i < db; ++i)
- {
- if(strcmp(tomb[i].megfigyelo_neve, nev) != 0)
- {
- if(strcmp(tomb[i].telepules_nev, tnev) != 0)
- {
- tomb2[j] = tomb[i];
- ++j;
- }
- }
- }
- tomb = tomb2;
- file_iras(tomb2);
- }
- void modositas(megfigyeles *tomb)
- {
- int i;
- char *nev;
- char *tnev;
- nev = (char *)malloc(charhossz);
- tnev = (char *)malloc(charhossz);
- printf("Adja meg a modositani kivant adatokat!\n");
- printf("Adja meg a keresett nevet!\n");
- scanf("%s", nev);
- printf("Adja meg a keresett telepulest!\n");
- scanf("%s", tnev);
- for(i = 0; i < db; ++i)
- {
- if(strcmp(tomb[i].megfigyelo_neve, nev) == 0)
- {
- if(strcmp(tomb[i].telepules_nev, tnev) == 0)
- {
- int darab = 0;
- printf("Adja meg a nyuszik szamat!\n");
- scanf("%d", &darab);
- tomb[i].nyuszi_db = darab;
- int val = -1;
- printf("Adja meg, milyen idos a nyul!\n");
- printf("(1) Gyerek\n");
- printf("(2) Felnott\n");
- printf("(3) Idos\n");
- while(val != 0)
- {
- scanf("%d", &val);
- switch(val)
- {
- case 1:
- tomb[i].nyuszi_kor = "GYEREK";
- val = 0;
- break;
- case 2:
- tomb[i].nyuszi_kor = "FELNOTT";
- val = 0;
- break;
- case 3:
- tomb[i].nyuszi_kor = "OREG";
- val = 0;
- break;
- default:
- printf("\n\nHIBAS ERTEKET ADOTT MEG\n\n");
- break;
- }
- }
- time_t datum;
- struct tm * ido;
- time (&datum);
- ido = localtime (&datum);
- strftime(tomb[i].megfigyelesi_ido,80,"%Y.%m.%d._%H:%M:%S",ido);
- }
- }
- }
- file_iras(tomb);
- }
- void menu()
- {
- printf("----------------------------------------------\n");
- printf("\n Valasszon az alabbi opciok kozul\n");
- printf("\t(1) - Megfigyeles rogzitese\n");
- printf("\t(2) - Megfigyeles modositasa\n");
- printf("\t(3) - Megfigyeles torlese\n");
- printf("\t(4) - Megfigyeles listazasa\n");
- printf("\t(0) - Kilepes\n");
- printf("----------------------------------------------\n>> ");
- }
- void rogzites(megfigyeles* tomb)
- {
- megfigyeles mf;
- char* nev;
- nev = (char *) malloc(charhossz);
- char* tnev;
- tnev = (char *) malloc(charhossz);
- int darab;
- mf.megfigyelesi_ido = (char *) malloc(charhossz);
- printf("Adja meg a megfigyelo nevet!\n");
- scanf("%s", nev);
- mf.megfigyelo_neve = nev;
- printf("Adja meg a telepules nevet!\n");
- scanf("%s", tnev);
- mf.telepules_nev = tnev;
- printf("Adja meg a nyuszik szamat!\n");
- scanf("%d", &darab);
- mf.nyuszi_db = darab;
- int val = -1;
- printf("Adja meg, milyen idos a nyul!\n");
- printf("(1) Gyerek\n");
- printf("(2) Felnott\n");
- printf("(3) Idos\n");
- while(val != 0){
- scanf("%d", &val);
- switch(val)
- {
- case 1:
- mf.nyuszi_kor = "GYEREK";
- val = 0;
- break;
- case 2:
- mf.nyuszi_kor = "FELNOTT";
- val = 0;
- break;
- case 3:
- mf.nyuszi_kor = "OREG";
- val = 0;
- break;
- default:
- printf("\n\nHIBAS ERTEKET ADOTT MEG\n\n");
- break;
- }
- }
- time_t datum;
- struct tm * ido;
- time (&datum);
- ido = localtime (&datum);
- strftime(mf.megfigyelesi_ido,80,"%Y.%m.%d._%H:%M:%S",ido);
- tomb[db] = mf;
- ++db;
- file_iras(tomb);
- //realloc//
- /*
- if(db >= (memoria/2))
- {
- printf("wtf\n");
- memoria += sizeof(megfigyeles);
- tomb = (megfigyeles *)realloc(tomb,memoria);
- }*/
- }
- void listazas(megfigyeles* tomb){
- int darab;
- int *darab_p = &darab;
- file_olvasas(tomb, darab_p);
- int i;
- for(i = 0; i < darab; ++i){
- printf("%s\t %s\t %d\t %s\t %s\n",
- tomb[i].megfigyelo_neve,
- tomb[i].telepules_nev,
- tomb[i].nyuszi_db,
- tomb[i].nyuszi_kor,
- tomb[i].megfigyelesi_ido);
- }
- db = darab;
- printf("\n");
- }
- int main(int argc, char** argv)
- {
- megfigyeles *tomb;
- tomb = (megfigyeles *)malloc(memoria);
- stat(argv[0],&st);
- printf("hellobello!\n");
- //menü
- char* valasz;
- valasz = (char*)malloc(charhossz);
- do{
- menu();
- scanf("%s", valasz);
- if(strcmp(valasz, "1") == 0){
- printf("\n\nRogzites\n\n");
- rogzites(tomb);
- }
- else if(strcmp(valasz, "2") == 0){
- printf("\n\nModositas\n\n");
- modositas(tomb);
- }
- else if(strcmp(valasz, "3") == 0){
- printf("\n\nTorles\n\n");
- torles(tomb);
- }
- else if(strcmp(valasz, "4") == 0){
- printf("\n\nListazas\n\n");
- listazas(tomb);
- }
- else if(strcmp(valasz, "0") == 0){
- }
- else{
- printf("\n\nHIBAS ERTEKET ADOTT MEG\n\n");
- }
- }while(strcmp(valasz, "0") != 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement