-3

I wrote these codes for my project but I have error in these codes. This function's purpose is to add new client in this file. When I process it, it's okay but if I want to see my clients from file I process search function it gets to me more than my clients like that. I tried to work it by using fscanf but when I tried it, my addition function isn't working completely because it is not read 'kisiler.iz' (means registration track). Like that

void ekle(struct kisiyapi kisiler)
{
    char secenek;
    dosya=fopen("kisiler.txt","a+");
    do
        {
        kisiler.iz=' '; 
        printf("Kisi no giriniz\n");
        scanf("%d",&kisiler.kisino);
        kaypos=(kisiler.kisino-1) * sizeof(kisiler);
        fseek(dosya,kaypos,SEEK_SET);
        fread(&kisiler,sizeof(kisiler),1,dosya);
        if(kisiler.iz!='*')
        {
        printf("Kisinin adini giriniz\n");
        scanf("%s",&kisiler.ad);
        printf("Kisinin soyadini giriniz\n");
        scanf("%s",&kisiler.soyad);
        printf("Kisinin Ev Telefonunu giriniz\n");
        scanf("%s",&kisiler.evtel);
        printf("Kisinin Cep Telefonunu giriniz\n");
        scanf("%s",&kisiler.ceptel);
        printf("Kisinin Is Telefonunu giriniz\n");
        scanf("%s",&kisiler.istel);
        printf("Kisinin Dogum tarihini sirasiyla gun/ay/yil olarak giriniz\n");
        scanf("%d",&kisiler.dogumgunu);
        scanf("%d",&kisiler.dogumayi);
        scanf("%d",&kisiler.dogumyili);
        printf("Kisinin yasadigi Il girin\n");
        scanf("%s",&kisiler.il);
        printf("Kisinin yasadigi Ilce girin\n");
        scanf("%s",&kisiler.ilce);
        printf("Kisinin yasadigi mahalleyi girin\n");
        scanf("%s",&kisiler.mahalle);
        printf("Kisinin yasadigi sokak no girin\n");
        scanf("%s",&kisiler.sokak); 
        kisiler.iz='*';
        fseek(dosya,kaypos,SEEK_SET);
        fwrite(&kisiler,sizeof(kisiler),1,dosya);
        }
        else
        {
            printf("Mevcut kayit var\n");
        }
        printf("\nTekrar ekleme yapmak istiyor musunuz ? [e veya h]");
        secenek=getch();

        }
        while(secenek!='h');
    fclose(dosya);
}

and these codes' purpose is update information of any client but it's not working because my feof loop isn't working or work forever. Therefore, I can't update my informations.

void guncelle(struct kisiyapi kisiler)
{

    char aranan[20];
    int i=0;
    dosya=fopen("kisiler.txt","r");
    yeni=fopen("temp.txt","w");
    if(dosya==NULL)
        printf("Dosyaya ulasilamadi\n");
    printf("Guncellemek istediginiz kisinin adini giriniz\n");
    scanf("%s",aranan);

    while(!feof(dosya))
    {
        i++;
    }
    /*
    while(!feof(dosya))
    {
        fread(&kisiler,sizeof(kisiler),1,dosya);
        kaypos=(kisiler.kisino-1) * sizeof(kisiler);
        if(strcmp(aranan,kisiler.ad)==0)
        {
            printf("Kisinin adini giriniz\n");
            scanf("%s",&kisiler.ad);
            printf("Kisinin soyadini giriniz\n");
            scanf("%s",&kisiler.soyad);
            printf("Kisinin Ev Telefonunu giriniz\n");
            scanf("%s",&kisiler.evtel);
            printf("Kisinin Cep Telefonunu giriniz\n");
            scanf("%s",&kisiler.ceptel);
            printf("Kisinin Is Telefonunu giriniz\n");
            scanf("%s",&kisiler.istel);
            printf("Kisinin Dogum tarihini sirasiyla gun/ay/yil olarak giriniz\n");
            scanf("%d",&kisiler.dogumgunu);
            scanf("%d",&kisiler.dogumayi);
            scanf("%d",&kisiler.dogumyili);
            printf("Kisinin yasadigi Il girin\n");
            scanf("%s",&kisiler.il);
            printf("Kisinin yasadigi Ilce girin\n");
            scanf("%s",&kisiler.ilce);
            printf("Kisinin yasadigi mahalleyi girin\n");
            scanf("%s",&kisiler.mahalle);
            printf("Kisinin yasadigi sokak no girin\n");
            scanf("%s",&kisiler.sokak); 
            kisiler.iz='*';
            fseek(yeni,kaypos,SEEK_SET);
            fprintf(dosya,"%s\t%s\t%s\t%s\t%s\t%d %d %d\t%s\t%s\t%s\t%s\t%c",kisiler.ad,kisiler.soyad,kisiler.evtel,kisiler.ceptel,kisiler.istel
            ,*kisiler.dogumgunu,*kisiler.dogumayi,*kisiler.dogumyili,kisiler.il,kisiler.ilce,kisiler.mahalle,kisiler.sokak,kisiler.iz);
        }

        else if(strcmp(aranan,kisiler.ad)!=0)
        {
            fseek(yeni,kaypos,SEEK_SET);
            fseek(dosya,kaypos,SEEK_SET);
            fwrite(&kisiler,sizeof(kisiler),1,yeni);
        }

    }

    fclose(dosya);
    fclose(yeni);
    */
}

If you understand my codes please help me I have no time to prepare it.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
B.Yalcin
  • 9
  • 1
  • 2
  • `while(!feof(dosya)) { i++; }` is almost certainly not going to produce the results you're telling us this code produces... Which book are you reading? – autistic Dec 23 '17 at 23:39
  • 1
    Saying "If you understand my codes please help me I have no time to prepare it" is not a good attitude — we're under no obligation to help you meet your homework time crunch. – Jonathan Leffler Dec 24 '17 at 01:13
  • I'm sorry I misspelled my English so bad. I meant I didn't have time. @JonathanLeffler – B.Yalcin Dec 23 '19 at 22:47

1 Answers1

2

The "eof" flag is only set once you try to read from beyond the end of the file. Your loop doesn't read anything from the file so it will never reach the end and will continue forever.

And very much related, you should read Why is “while ( !feof (file) )” always wrong?.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621