如何将行号仅添加到文件中的特定行

时间:2019-04-22 23:22:47

标签: c

我正在创建疾病的输入及其相关信息。该疾病旁边应有一个数字,但其余输入应保持不变。我如何将信息添加到文件中,但是随着程序关闭,在读取新输入时,疾病编号会增加?

我尝试使用变量来打印疾病编号,但我不知道如何增加它。

void CreateNew(){

    int diseasenum=1;
    FILE*fptr;

    fptr = fopen("Lifeline Medical & Diagnostic Center.txt", "a+");

    if(fptr == NULL)
    {
        printf("Error! There is no file to write to. Please Create a file");
        exit(1);
    }

    fflush(stdin);

    printf("Enter the name of the disease you would like to give detail of: ");
    gets(Dissarray.Disease);

    if()
        fprintf(fptr,"%d\tDisease: %s\n\n",diseasenum,Dissarray.Disease);

    printf("\n");

    fflush(stdin);

    Dissarray.Lethality=0;

    printf("What is the Lethality of %s?\t(Answer in percentage. Sample:90 OR 12, etc)\n",Dissarray.Disease);
    if (scanf("%d", &Dissarray.Lethality)!= 1)
    {
      printf("This is not an appropriate number. Please enter appropriately.\n");
      fflush(stdin);
      scanf("%d", &Dissarray.Lethality);
    }
    fprintf(fptr,"Lethality: %d\n",Dissarray.Lethality);
    printf("\n");

    fflush(stdin);

    printf("How is %s acquired. (Sample: Contagious Disease, STI, Hereditary)\n",Dissarray.Disease);
    gets(Dissarray.ContagionFactor);
    fprintf(fptr,"Contagion factor: %s\n",Dissarray.ContagionFactor);
    printf("\n");

    printf("How is %s Transmitted?\t\t(Sample: Airborne, Touch, Sex, Sneezing, etc.)\n",Dissarray.Disease);
    gets(Dissarray.Spread);
    fprintf(fptr,"Spread: %s\n",Dissarray.Spread);
    printf("\n");

    fflush(stdin);

    Dissarray.Fatalities=0;

    printf("On a yearly basis. What is the average Fatality count brought by %s?\t\t(How many have died to this disease? Sample:100000)\n",Dissarray.Disease);
    if(scanf("%d", &Dissarray.Fatalities)!= 1)
    {
      printf("This is not an appropriate number. Please enter appropriately.\n");
      fflush(stdin);
      scanf("%d", &Dissarray.Fatalities);
    }
    fprintf(fptr,"Fatalities: %d\n",Dissarray.Fatalities);
    printf("\n");

    fflush(stdin);

    printf("Has %s been known to evolve under any conditions?\n",Dissarray.Disease);
    printf("What is the Sensitivity?\t\t(Sample:Temperatures over 90 degrees OR None.)\n");
    gets(Dissarray.Sensitivity);
    fprintf(fptr,"Sensitivity: %s\n\n",Dissarray.Sensitivity);
    printf("\n");

    printf("This ends the entry of info into the file\n");
    fclose(fptr);
}

1 个答案:

答案 0 :(得分:0)

回答有关行号的问题:

关于:

int diseasenum=1;

更改为:

static int diseasenum=1;

然后,在退出功能之前:

diseasenum++;