Home > Software design >  How do you delete certain strings from a textfile based on user input in c
How do you delete certain strings from a textfile based on user input in c

Time:06-20

i have to code a hospital managment system in c for my programming class. The patients list and admitting patients works just fine, but I cant discharge patients properly. My programm either isn't able to find patients based on their ID or it discharges mutiple patients at once. There are no warnings or errors when I try to run it. My current approach is using strcmp to compare the ID of the patient to the an ID entered by a user.

Here is my code:

#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <windows.h>
#define MAX 99


struct patient{
    char raum[MAX];
    char nummer[MAX];
    char alter[MAX];
    char name[MAX];
    char geschlecht[MAX];
    char krankheit[MAX];
}p;

int main(void){
    FILE *akten;
    int eingabe;
do{
    printf("Krankenhausinformationssystem:\n");
    printf("1.Patientenliste.\n");
    printf("2.Patienten hinzufuegen.\n");
    printf("3.Raumzuteilung.\n");
    printf("4.Patienten entlassen.\n");
    printf("5.Beenden.\n");
    scanf("%i", &eingabe);
    
    switch(eingabe){
        case 1: printf("Patientenliste:\n");
    printf("%-10s %-20s %-20s %-20s %-20s %s\n", "Nummer", " Raum", "Name", "Geschlecht", "Alter", "Krankheit");
    printf("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");

    akten = fopen("akten.txt", "rb");
    
    while(fread(&p, sizeof(p), 1, akten) == 1){
        printf("%-10s %-20s %-20s %-20s %-20s %s\n", p.raum, p.nummer, p.alter,p.name,p.geschlecht,p.krankheit);
    }

    fclose(akten);break;
        
        
        case 2: printf("Patienten hinzufuegen:\n");
        akten = fopen("akten.txt","ab");
        printf("Nummer:\n");
        fflush(stdin);
        scanf("%s",p.nummer);
         printf("Raum:\n");
          fflush(stdin);
         gets(p.raum);
          printf("Name:\n");
           fflush(stdin);
          gets(p.name);
           printf("Geschlecht:\n");
            fflush(stdin);
           gets(p.geschlecht);
            printf("Alter:\n");
             fflush(stdin);
            gets(p.alter);
             printf("Krankheit:\n");
              fflush(stdin);
             gets(p.krankheit);
             fwrite(p.nummer, sizeof(p.nummer), 1, akten);
              fwrite(p.raum, sizeof(p.raum), 1, akten);
               fwrite(p.name, sizeof(p.name), 1, akten);
                fwrite(p.geschlecht, sizeof(p.geschlecht), 1, akten);
                 fwrite(p.alter, sizeof(p.alter), 1, akten);
                  fwrite(p.krankheit, sizeof(p.krankheit), 1, akten);
             fclose(akten);break;
        
   
        
        case 4: printf("Patienten entlassen:\n");
    
            char nummer[MAX];
            int f=0;
            printf("Patientennummer:\n ");
            fflush(stdin);
            scanf("%s", nummer);

            FILE *ft;

            akten = fopen("akten.txt", "rb");
            ft = fopen("temp.txt", "wb");

            while(fread(&p, sizeof(p), 1, akten) == 1){
                //strcpy(*nummer); strcpy(*p.nummer);
      //int vergleich = strcmp(p.nummer,nummer);
            if(nummer  == p.nummer){
            f=1;
            }else{
            fwrite(&p, sizeof(p), 1, ft);
  }
}


    if(f==1){
        printf("Patient wurde entlassen.\n");
    }else{
        printf("Patient nicht gefunden !\n");
    }

    fclose(akten);
    fclose(ft);

    remove("akten.txt");
    rename("temp.txt", "akten.txt");

break;
        case 5: exit(0);
        default: printf("Ungueltige Eingabe");break;
   
    }}
while(eingabe != 5); return 0;} ```

CodePudding user response:

I tested out your code and from that testing, I have a few observations and suggestions. First, since your patient number entry is actually a string, I changed the "if" test to use the "strcmp" function as noted in the comments.

/* if(nummer  == p.nummer){ */
if((strcmp(nummer, p.nummer) == 0)) {

I still seemed to have difficulties getting that function to work until I revised your output of new patient data to just write out the patient structure to your file and not the individual elements.

fwrite(&p, sizeof(p), 1, akten);

Also, just to be consistent, I refactored all of the "gets" functions with similar "scanf" functions instead of having a mixture. Following was one such entry.

scanf("%s", p.name);

That consistency seemed to allow for the discharge function to work. Go ahead and give that a try. If you need to see more of the code in any of the functions, let me know.

Hope that helps.

Regards.

  • Related