Binary adress book-minor syntax -- mitola

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mitola
    New Member
    • Sep 2007
    • 17

    Binary adress book-minor syntax -- mitola

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>


    struct oseba
    {
    char ime[20];
    char priimek[15];
    char ulica[50];
    char mesto[50];
    char tel_st[10];
    short ucni_uspeh;
    }pod;

    void vnos(struct oseba pod,FILE *fd)
    {
    printf("Vnesi ime: "); scanf("%s",pod. ime);
    printf("Vnesi priimek: "); scanf("%s",pod. priimek);
    printf("Vnesi ucni uspeh: "); scanf("%d",&pod .ucni_uspeh);
    printf("Vnesi ulico in st.: "); fgets(pod.ulica ,100,fd); //scanf("%s",pod. ulica);
    printf("Vnesi ime mesta: "); scanf("%s",pod. mesto);
    printf("Vnesi tel st: "); scanf("%s",pod. tel_st);
    fd=fopen("DIJAK I.BIN","a");
    fwrite(&pod,siz eof(pod),1,fd);
    fclose(fd);
    }

    void izpis(struct oseba pod,FILE *fd)
    {
    fd=fopen("DIJAK I.BIN","r");
    fread(&pod,size of(pod),1,fd);
    fclose(fd);
    printf("%s %s\n", pod.ime, pod.priimek);
    printf("Ucni uspeh: %d\n",pod.ucni_ uspeh);
    printf("Ulica: %s\n",pod.ulica );
    printf("Mesto: %s\n",pod.mesto );
    printf("Telefon ska stevilka: %s\n",pod.tel_s t);
    }
    int main(void)
    {
    FILE *fd;
    int a;
    int menu;
    printf("------------------------------------------------------------------------------- \n");
    printf(" IMENIK \n");
    printf("------------------------------------------------------------------------------- \n");
    printf("Vnesi izbiro:\n");
    printf("1.)Vpis podatkov\n");
    printf("2.)Izpi s vseh podatkov\n");
    printf("3.)Iska nje po imenu ali priimku\n");



    scanf("%d",&men u);
    switch(menu)
    {
    case 1: vnos(pod,fd);
    break;
    case 2: izpis(pod,fd);
    break;
    }

    system("pause") ;
    return 0;
    }


    heres the code :

    void vnos(struct oseba pod,FILE *fd)
    {
    printf("Vnesi ime: "); scanf("%s",pod. ime);
    printf("Vnesi priimek: "); scanf("%s",pod. priimek);
    printf("Vnesi ucni uspeh: "); scanf("%d",&pod .ucni_uspeh);
    printf("Vnesi ulico in st.: "); fgets(pod.ulica ,100,fd);//here is the problem
    //scanf("%s",pod. ulica);
    printf("Vnesi ime mesta: "); scanf("%s",pod. mesto);
    printf("Vnesi tel st: "); scanf("%s",pod. tel_st);
    fd=fopen("DIJAK I.BIN","a");
    fwrite(&pod,siz eof(pod),1,fd);
    fclose(fd);
    }

    well i have problem when trying to scan a string longer then one word for example: railroad 25
    with normal scan i can rea only first word and i dont know how to correct this mistake so im asking for help.
  • Cucumber
    New Member
    • Sep 2007
    • 90

    #2
    Use
    Code:
    gets();
    or

    Code:
    scanf("%[^\n]s",s);

    Comment

    • mitola
      New Member
      • Sep 2007
      • 17

      #3
      void vnos(struct oseba pod,FILE *fd)
      {
      printf("Vnesi ime: "); scanf("%s",pod. ime);
      printf("Vnesi priimek: "); scanf("%s",pod. priimek);
      printf("Vnesi ucni uspeh: "); scanf("%d",&pod .ucni_uspeh);
      printf("Vnesi ulico in st: ");scanf("%[^\n]s",pod.ulica );
      printf("Vnesi ime mesta: "); scanf("%s",pod. mesto);
      printf("Vnesi tel st: "); scanf("%s",pod. tel_st);
      fd=fopen("DIJAK I.BIN","a");
      fwrite(&pod,siz eof(pod),1,fd);
      fclose(fd);
      }

      i tryed that what you said and it dont wrote any syntax but just automaticly skips the : scanf("%[^\n]s",pod.ulica );

      so how can i fix that i'll be able to actually wrote an ino there

      Comment

      • Cucumber
        New Member
        • Sep 2007
        • 90

        #4
        use fflush(stdin)

        Code:
        void vnos(struct oseba pod,FILE *fd)
        {
        	printf("Vnesi ime: ");			scanf("%s",pod.ime);
                        fflush(stdin);
        	printf("Vnesi priimek: ");		scanf("%s",pod.priimek);
                        fflush(stdin);
        	printf("Vnesi ucni uspeh: ");	scanf("%d",&pod.ucni_uspeh);
                        fflush(stdin);
        	printf("Vnesi ulico in st: ");scanf("%[^\n]s",pod.ulica);
                        fflush(stdin);
        	printf("Vnesi ime mesta: ");	scanf("%s",pod.mesto);
                        fflush(stdin);
        	printf("Vnesi tel st: ");		scanf("%s",pod.tel_st);
                        fflush(stdin);
        	fd=fopen("DIJAKI.BIN","a");
        	fwrite(&pod,sizeof(pod),1,fd);
        	fclose(fd);
        }

        Comment

        Working...