This is my function UpdateWord. It searches for a word in the file and updates the word or the meaning

void UpdateWord(void)
{
     char srchWrd[30],choice,qtn='Y';
     int x,y;
     dictio a, temp;
     FILE*ptr;
     do
     {
        printf("\nEnter Word to update: "); fflush(stdin);
        gets(srchWrd);

        x=strlen(srchWrd);
        for(y=0;y<x;y++)
          srchWrd[x] = tolower(srchWrd[x]);

        if((srchWrd[0]>='a')&&(srchWrd[0]<='i'))
           ptr=fopen("A-I.dat","r+b");
        else if((srchWrd[0]>'i')&&(srchWrd[0]<'s'))
           ptr=fopen("J-R.dat","r+b");
        else
           ptr=fopen("S-Z.dat","r+b");

        while(fread(&a,sizeof(dictio),1,ptr))
        {

         system("cls");
         if(strcmp(srchWrd,a.word)==0)
         {
            printf("What will you change? (W)-Word (M)-Meaning: ");fflush(stdin);
            fflush(stdin); scanf("%c",&choice);


            switch(choice)
          {
            case 'W':
            case 'w': printf("Enter New word: ");fflush(stdin);gets(temp.word);break;
            case 'M':
            case 'm': printf("Enter New meaning: ");fflush(stdin);gets(a.meaning);break;
            default: printf("Invalid option. Enter Again"); break;
          }
          }

        fseek(ptr,sizeof(dictio)*-1,1);
        fwrite(&a,sizeof(dictio),1,ptr);
        }


     printf("\nDo you still want to edit?(Y/N): "); scanf("%c",&qtn);
     }
     while(qtn!='N'&&qtn!='n');
     fclose(ptr);
     getch();
}  

I'm having problems with this fuction. If I update a word, it just keeps on writing without stopping. I didn't notice it at fist but when I found out that my file was already at 1.79GB, i closed my program and deleted my file. Help me please

move lines 43 and 44 up between lines 40 and 41 so that they are within the if statement that start on line 27. The way it is now you are re-writing the struture whether it needs to be changed or not. Only write the structure if something has been changed.

This article has been dead for over six months. Start a new discussion instead.