Advertisement
t0shec

Untitled

Oct 17th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. struct LICE {
  6.     char ime[31]; char EGN[11];
  7. };
  8.  
  9. FILE *fl;
  10. char imefl[30];
  11. LICE lice;
  12.  
  13. void readLice(LICE *lc) {
  14.     printf("%20s IME: ","");
  15.     gets(lc->ime);
  16.     printf("%20s ENG: ","");
  17.     gets(lc->EGN);
  18. }
  19.  
  20. void writeLice(LICE lc) {
  21.     printf("IME: %-21s EGN: %s\n",lc.ime,lc.EGN);
  22. }
  23.  
  24. int exist(char *ime){
  25.     FILE *f = fopen(ime,"r"); fclose(f);
  26.     return f!=NULL;
  27. }
  28.  
  29. void szdPrz () {
  30.     char ch;
  31.     if (exist(imefl)) {
  32.         printf("File exists!"); return;
  33.         printf("Rewrite?");
  34.         ch=getchar();
  35.         if (ch == 'n') return;
  36.     }
  37.     fl=fopen(imefl,"w");
  38.     fclose(fl);
  39.     printf("\n File Created. \n");
  40. }
  41.  
  42. void dobaviane(){
  43.     char ch;
  44.     if (!exist(imefl)) {
  45.         printf("File exists!\n"); return;
  46.     }
  47.     fl=fopen(imefl,"a+b");
  48.     do{
  49.         readLice(&lice);
  50.         fwrite(&lice,sizeof(lice),1,fl);
  51.         printf("Continue?");
  52.         ch=getchar();getchar();
  53.     }
  54.     while (ch != 'n');
  55.     fclose(fl);
  56.    
  57. }
  58.  
  59. void iztrivane() {
  60.     FILE *delFl;
  61.     char EGN[11];
  62.     if (!exist(imefl)) {
  63.         printf("File not created!\n"); return;
  64.     }
  65.     printf("EGN: "); gets(EGN);
  66.     fl=fopen(imefl,"rb"); delFl=fopen("Del","wb");
  67.     fread(&lice,sizeof(lice),1,fl);
  68.     while (!feof(fl)) {
  69.         if (strcmp(lice.EGN,EGN) != 0)
  70.         fwrite(&lice,sizeof(lice),1,delFl);
  71.     else printf("Data erased!\n");
  72.     fread(&lice,sizeof(lice),1,fl);
  73.     }
  74.     fclose(fl);fclose(delFl);
  75.     remove(imefl);rename("Del",imefl);
  76. }
  77.  
  78. void zamiana() {
  79.     char EGN[11];
  80.     if (!exist(imefl)){
  81.         printf("File not created!\n"); return;
  82.     }
  83.     printf("EGN: "); gets(EGN);
  84.     fl=fopen(imefl,"r+b");
  85.     fread(&lice,sizeof(lice),1,fl);
  86.     while (!feof(fl)){
  87.         if (strcmp(lice.EGN,EGN) == 0) {
  88.             readLice(&lice);
  89.             fseek(fl,-(long)sizeof(lice),SEEK_CUR);
  90.             fwrite(&lice,sizeof(lice),1,fl);
  91.             printf("Data rewrited!\n",EGN);
  92.             break;
  93.         }
  94.         fread(&lice,sizeof(lice),1,fl);
  95.     }
  96.     fclose(fl); printf("\n");
  97. }
  98.  
  99. void spisGod() {
  100.     int k,godTrs,godLice;
  101.     char gg[3];
  102.     if (!exist(imefl)) {
  103.         printf("File not created!\n"); return;
  104.     }
  105.     printf("Year of birth: "); scanf("%d",&godTrs); getchar();
  106.     fl=fopen(imefl,"rb"); k=0;
  107.     fread(&lice,sizeof(lice),1,fl);
  108.     while (!feof(fl)) {
  109.         strncpy(gg,lice.EGN,2); gg[2]=0; godLice=atoi(gg);
  110.         if (lice.EGN[2]>'3') godLice+=2000;
  111.         else godLice+=1900;
  112.         if (godLice==godTrs) {
  113.             k++;writeLice(lice);
  114.         }
  115.         fread(&lice,sizeof(lice),1,fl);
  116.     }
  117.     if (k==0)
  118.     printf("Noone born in %d year!\n",godTrs);
  119.     fclose(fl);
  120. }
  121.  
  122. void obSpis(){
  123.     int k;
  124.     if (!exist(imefl)){
  125.         printf("File not created!\n"); return;
  126.     }
  127.     fl=fopen(imefl,"rb"); k=0;
  128.     fread(&lice,sizeof(lice),1,fl);
  129.     while (!feof(fl)){
  130.         k++;writeLice(lice);
  131.         fread(&lice,sizeof(lice),1,fl);
  132.     }
  133.     if (k==0) printf("File is empty!\n");
  134.     fclose(fl);
  135. }
  136.  
  137. void main(){
  138.     int c;
  139.     printf("Filename: "); gets(imefl);
  140.     do {
  141.         printf("%20s Menu:\n");
  142.         printf("%20s 1.Create file'\n'");
  143.         printf("%20s 2.Add entry\n");
  144.         printf("%20s 3.Delete component\n");
  145.         printf("%20s 4.Rewrite component\n");
  146.         printf("%20s 5.Born X year\n");
  147.         printf("%20s 6.List all\n");
  148.         printf("%20s 0.Exit");
  149.         scanf("%d",&c); getchar();
  150.         switch (c) {
  151.             case 1:szdPrz(); break;
  152.             case 2:dobaviane(); break;
  153.             case 3:iztrivane(); break;
  154.             case 4:zamiana(); break;
  155.             case 5:spisGod(); break;
  156.             case 6:obSpis();
  157.         }
  158.     }while (c!=0);
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement