Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- struct LICE {
- char ime[31]; char EGN[11];
- };
- FILE *fl;
- char imefl[30];
- LICE lice;
- void readLice(LICE *lc) {
- printf("%20s IME: ","");
- gets(lc->ime);
- printf("%20s ENG: ","");
- gets(lc->EGN);
- }
- void writeLice(LICE lc) {
- printf("IME: %-21s EGN: %s\n",lc.ime,lc.EGN);
- }
- int exist(char *ime){
- FILE *f = fopen(ime,"r"); fclose(f);
- return f!=NULL;
- }
- void szdPrz () {
- char ch;
- if (exist(imefl)) {
- printf("File exists!"); return;
- printf("Rewrite?");
- ch=getchar();
- if (ch == 'n') return;
- }
- fl=fopen(imefl,"w");
- fclose(fl);
- printf("\n File Created. \n");
- }
- void dobaviane(){
- char ch;
- if (!exist(imefl)) {
- printf("File exists!\n"); return;
- }
- fl=fopen(imefl,"a+b");
- do{
- readLice(&lice);
- fwrite(&lice,sizeof(lice),1,fl);
- printf("Continue?");
- ch=getchar();getchar();
- }
- while (ch != 'n');
- fclose(fl);
- }
- void iztrivane() {
- FILE *delFl;
- char EGN[11];
- if (!exist(imefl)) {
- printf("File not created!\n"); return;
- }
- printf("EGN: "); gets(EGN);
- fl=fopen(imefl,"rb"); delFl=fopen("Del","wb");
- fread(&lice,sizeof(lice),1,fl);
- while (!feof(fl)) {
- if (strcmp(lice.EGN,EGN) != 0)
- fwrite(&lice,sizeof(lice),1,delFl);
- else printf("Data erased!\n");
- fread(&lice,sizeof(lice),1,fl);
- }
- fclose(fl);fclose(delFl);
- remove(imefl);rename("Del",imefl);
- }
- void zamiana() {
- char EGN[11];
- if (!exist(imefl)){
- printf("File not created!\n"); return;
- }
- printf("EGN: "); gets(EGN);
- fl=fopen(imefl,"r+b");
- fread(&lice,sizeof(lice),1,fl);
- while (!feof(fl)){
- if (strcmp(lice.EGN,EGN) == 0) {
- readLice(&lice);
- fseek(fl,-(long)sizeof(lice),SEEK_CUR);
- fwrite(&lice,sizeof(lice),1,fl);
- printf("Data rewrited!\n",EGN);
- break;
- }
- fread(&lice,sizeof(lice),1,fl);
- }
- fclose(fl); printf("\n");
- }
- void spisGod() {
- int k,godTrs,godLice;
- char gg[3];
- if (!exist(imefl)) {
- printf("File not created!\n"); return;
- }
- printf("Year of birth: "); scanf("%d",&godTrs); getchar();
- fl=fopen(imefl,"rb"); k=0;
- fread(&lice,sizeof(lice),1,fl);
- while (!feof(fl)) {
- strncpy(gg,lice.EGN,2); gg[2]=0; godLice=atoi(gg);
- if (lice.EGN[2]>'3') godLice+=2000;
- else godLice+=1900;
- if (godLice==godTrs) {
- k++;writeLice(lice);
- }
- fread(&lice,sizeof(lice),1,fl);
- }
- if (k==0)
- printf("Noone born in %d year!\n",godTrs);
- fclose(fl);
- }
- void obSpis(){
- int k;
- if (!exist(imefl)){
- printf("File not created!\n"); return;
- }
- fl=fopen(imefl,"rb"); k=0;
- fread(&lice,sizeof(lice),1,fl);
- while (!feof(fl)){
- k++;writeLice(lice);
- fread(&lice,sizeof(lice),1,fl);
- }
- if (k==0) printf("File is empty!\n");
- fclose(fl);
- }
- void main(){
- int c;
- printf("Filename: "); gets(imefl);
- do {
- printf("%20s Menu:\n");
- printf("%20s 1.Create file'\n'");
- printf("%20s 2.Add entry\n");
- printf("%20s 3.Delete component\n");
- printf("%20s 4.Rewrite component\n");
- printf("%20s 5.Born X year\n");
- printf("%20s 6.List all\n");
- printf("%20s 0.Exit");
- scanf("%d",&c); getchar();
- switch (c) {
- case 1:szdPrz(); break;
- case 2:dobaviane(); break;
- case 3:iztrivane(); break;
- case 4:zamiana(); break;
- case 5:spisGod(); break;
- case 6:obSpis();
- }
- }while (c!=0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement