简体   繁体   English

如何根据 c 中的用户输入从文本文件中删除某些字符串

[英]How do you delete certain strings from a textfile based on user input in c

i have to code a hospital managment system in c for my programming class.我必须为我的编程课用 c 编写一个医院管理系统。 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.我的程序要么无法根据他们的 ID 找到患者,要么一次让多名患者出院。 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.我目前的方法是使用 strcmp 将患者的 ID 与用户输入的 ID 进行比较。

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;} ```

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”测试更改为使用注释中所述的“strcmp”函数。

/* 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.此外,为了保持一致,我用类似的“scanf”函数重构了所有“gets”函数,而不是混合使用。 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.问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C语言中,我如何只接受某些字符串并继续要求用户输入,直到输入了有效的输入? - In C, how do I only accept certain strings and continue to ask the user for input until valid input is entered? 如何在 C 中将字符串与不确定的用户输入分开? - How do I separate strings from uncertain user input in C? 如何在混合了Strings和Integer的文本文件中获得INTEGER? - How do you get an INTEGER in a textfile mixed with Strings and Integer? 如何根据用户输入在 C 中使用 for 循环打印多个字符串 - How do I print multiple strings based on user input using for loop in C C-如何(通过基于用户的输入)将各种字符串存储在结构内部的数组中? - C - How can I store (from user-based input) various strings in an array inside a structure? ANSI C:您如何从用户那里获取输入然后反向打印? - ANSI C: How do you ingest input from user and then print it in reverse? C:您如何处理没有用户输入的scanf? - C: How do you handle for no user input for scanf? C:如何将用户输入限制为2个以空格分隔的参数? - C: How do you limit user input to 2 arguments separated by a space? 如何将用户输入存储为c中的变量? - How do you store user input as a variable in c? 如何使用用户输入打开FILE并将其放入C语言的字符串中 - How do you open a FILE with the user input and put it into a string in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM