By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,588 Members | 1,920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,588 IT Pros & Developers. It's quick & easy.

c binary searching for adress mitola

P: 17
Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <conio.h>
  5.  
  6.  
  7. struct oseba //struktura osebe , ki jo kasneje uporabljamo v datoteki
  8. {
  9.     char ime[20];
  10.     char priimek[15];
  11.     char ulica[50];
  12.     char mesto[50];
  13.     char tel_st[10];
  14.     short ucni_uspeh;
  15. }pod[100];
  16.  
  17.  
  18. void vnos(struct oseba pod,FILE *fd)//vnos osebe
  19. {int i;int n;
  20.  
  21.     //for (i=0;i<n;i++)
  22.     printf("Vnesi ime: ");            fflush(stdin);    scanf("%s",pod.ime);
  23.     printf("Vnesi priimek: ");        fflush(stdin);    scanf("%s",pod.priimek);
  24.     printf("Vnesi ucni uspeh: ");    fflush(stdin);    scanf("%d",&pod.ucni_uspeh);
  25.     printf("Vnesi ulico in st: ");    fflush(stdin);    scanf("%s",pod.ulica);
  26.     printf("Vnesi ime mesta: ");    fflush(stdin);    scanf("%s",pod.mesto);
  27.     printf("Vnesi tel st: ");        fflush(stdin);    scanf("%s",pod.tel_st);
  28.  
  29. fd=fopen("DIJAKI.BIN","a"); //
  30. //fseek(fd,0,SEEK_END);
  31. fwrite(&pod,sizeof(pod),1,fd);
  32. fclose(fd);
  33. }
  34.  
  35. void izpis(struct oseba pod,FILE *fd)//izpis osebe
  36. {  
  37.     fd=fopen("DIJAKI.BIN","r");
  38.  
  39.     //fseek(fd,sizeof(struct pod)*1,SEEK_SET);      
  40.  
  41. while(!feof(fd))
  42.     {              //fread(&r,sizeof(struct rec),1,f);   
  43.         fread(&pod,sizeof(pod),1,fd);
  44.         printf("%s %s\n", pod.ime, pod.priimek);
  45.         printf("Ucni uspeh: %d\n",pod.ucni_uspeh);
  46.         printf("Ulica: %s\n",pod.ulica);
  47.         printf("Mesto: %s\n",pod.mesto);
  48.         printf("Telefonska stevilka: %s\n\n\n",pod.tel_st);
  49.     }
  50.     fclose(fd);
  51. system("pause");
  52. }
  53.  
  54. void iskanje(struct oseba pod,FILE *fd)//pri temule delu potrebujem pomoc matic.
  55.                                         //kako naj pri izbiri ena naredim da najde pac string pa potem
  56.                                         //ga obdelamo oz. izbrisemo
  57. {int a;char iskano[20];
  58.         fd=fopen("DIJAKI.BIN","r");
  59.  
  60. printf("Ali zelis iskati po priimku ali po imenu:\n\n1.) Po imenu\n\n2.) Po priimku\n\n3.) izhod v meni\n\n");
  61. scanf("%d",&a);
  62. system("cls");
  63. if(a==1)
  64.     {
  65.         printf("\n\nvnesi ime:\n");
  66.         scanf("%s",iskano);
  67.         while(!feof(fd))
  68.             {              //fread(&r,sizeof(struct rec),1,f);   
  69.                 fread(&pod,sizeof(pod),1,fd);
  70.                 if(strcmp (pod.ime,iskano) == 0);// namesto tega probaj dat primerjavo stringov ne strcm,p al pa kej a=b neki probi-***************
  71.                 {system("pause");/*predelaj oz. odstrani ime*//* uredi()*/}
  72.             }
  73.     }
  74.  
  75. if(a==2)
  76.     {
  77.         printf("\n\nvnesi priimek:\n");
  78.         while(!feof(fd))
  79.             {              //fread(&r,sizeof(struct rec),1,f);   
  80.                 /*fread(&pod,sizeof(pod),1,fd);
  81.                 printf("%s %s\n", pod.ime, pod.priimek);
  82.                 printf("Ucni uspeh: %d\n",pod.ucni_uspeh);
  83.                 printf("Ulica: %s\n",pod.ulica);
  84.                 printf("Mesto: %s\n",pod.mesto);
  85.                 printf("Telefonska stevilka: %s\n\n\n",pod.tel_st);*/
  86.             }
  87.     }
  88.  
  89. if(a==3)
  90. {exit(1);}
  91.  
  92. else {system("cls");printf("Napacn vnos. Prosim poskusite znova\n\n\n");iskanje(pod,fd);}
  93.     fclose(fd);
  94. }
  95.  
  96.  
  97. int main(void)
  98. {
  99. FILE *fd;
  100. int a;
  101. int menu;
  102.  
  103. for(;;)
  104. {
  105.     printf("------------------------------------------------------------------------------- \n");
  106.     printf("                                     IMENIK                                     \n");
  107.     printf("------------------------------------------------------------------------------- \n");
  108.     printf("Vnesi izbiro:\n");
  109.     printf("1.)Vpis podatkov\n");
  110.     printf("2.)Izpis vseh podatkov\n");
  111.     printf("3.)Iskanje po imenu ali priimku\n");
  112.     printf("4.)Koncaj program\n\n");
  113.  
  114.     printf("Vnesi izbiro:   ");
  115.     scanf("%d",&menu);
  116.         switch(menu)
  117.         {
  118.             case 1:    vnos(pod[0],fd);
  119.                     break;
  120.             case 2:    izpis(pod[0],fd);
  121.                     break;
  122.             case 3:    iskanje(pod[0],fd);
  123.                     break;
  124.             case 4: exit(1);        
  125.                     //uredi iskanje, in dodaj sortiranje ;shell-sort,quicksort ali bubble sort
  126.         }
  127. }
  128.     return system("pause");
  129. }
  130.  
  131.  
  132. /*
  133. char znak = getch(a);
  134. printf ("%d", znak,znak);*/











in this part i dont know how to compare strings right beacuse strcmp dont give me right results. also if you have any good idea how would be easiest to delete certain names from this adress book together with their details.

the program is adress book with name,last name , city , state etc.

Expand|Select|Wrap|Line Numbers
  1. void iskanje(struct oseba pod,FILE *fd)//pri temule delu potrebujem pomoc matic.
  2.                                         //kako naj pri izbiri ena naredim da najde pac string pa potem
  3.                                         //ga obdelamo oz. izbrisemo
  4. {int a;char iskano[20];
  5.         fd=fopen("DIJAKI.BIN","r");
  6.  
  7. printf("Ali zelis iskati po priimku ali po imenu:\n\n1.) Po imenu\n\n2.) Po priimku\n\n3.) izhod v meni\n\n");
  8. scanf("%d",&a);
  9. system("cls");
  10. if(a==1)
  11.     {
  12.         printf("\n\nvnesi ime:\n");
  13.         scanf("%s",iskano);
  14.         while(!feof(fd))
  15.             {              //fread(&r,sizeof(struct rec),1,f);   
  16.                 fread(&pod,sizeof(pod),1,fd);
  17.                 if(strcmp (pod.ime,iskano) == 0);// namesto tega probaj dat primerjavo stringov ne strcm,p al pa kej a=b neki probi-***************
  18.                 {system("pause");/*predelaj oz. odstrani ime*//* uredi()*/}
  19.             }
  20.     }
Nov 30 '07 #1
Share this Question
Share on Google+
1 Reply


mschenkelberg
P: 44
I think the problem is fread into your struct. Since all of your data in the struct is fixed size, fread will just read up to sizeof bytes filling up your struct starting with the first char array, once that array is filled it will read into the next one and so on. Also if fread was working, you would need to account for NULL termination before you can use strcmp it counts on it! I would recommend doing a fscanf if you know the order the data will be in in the file, or fgets if it is a line at a time. fscanf will automatically NULL terminate and fgets will not. Be sure to NULL terminate strings before using string functions that don't have the 'n' in them.
Nov 30 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.