c - 为什么每次我再次编译程序时都需要在文件中添加相同的元素

标签 c

我可以在元素周期表中添加新元素,并将其添加到文件中。但问题是,当我下次编译后搜索新插入的元素时,但它说找不到特定元素在一个文件中。为什么elememt没有永久添加到文件中?

#include<stdio.h>
#include<conio.h>
#include<string.h>

typedef struct element
{
     char nm[15];
     char sym[3];
     char blk[15];
     int atmno;
     float mass;
     char cat[18];
}el;

FILE *fp;
int i,tot=45;
el e[80];
void home();
void search();
void add();

int main()
{

    int i;
    fp=fopen("per.txt","w");
    if(fp==NULL)
    {
        printf("\nerror opening file");
        return 0;
    }
    //1st element
    strcpy(e[0].nm,"Hydrogen");
    strcpy(e[0].blk,"Not Specified");
    strcpy(e[0].sym,"H");
    e[0].atmno=1;
    e[0].mass=2;
    strcpy(e[0].cat,"Non Metal");
    //2nd element
        strcpy(e[1].nm,"Lithium");
    strcpy(e[1].blk,"s");
    strcpy(e[1].sym,"Li");
    e[1].atmno=3;
    e[1].mass=6.94;
    strcpy(e[1].cat,"Metal");
    //3rd element
        strcpy(e[2].nm,"Berillium");
    strcpy(e[2].blk,"s");
    strcpy(e[2].sym,"Be");
    e[2].atmno=4;
    e[2].mass=9.02;
    strcpy(e[2].cat,"Metal");
    //4th element
    strcpy(e[3].nm,"Sodium");
    strcpy(e[3].blk,"s");
    strcpy(e[3].sym,"Na");
    e[3].atmno=11;
    e[3].mass=22.99;
    strcpy(e[3].cat,"Metal");
    //5th elemnt
    strcpy(e[4].nm,"Magnesium");
    strcpy(e[4].blk,"s");
    strcpy(e[4].sym,"Mg");
    e[4].atmno=12;
    e[4].mass=24.31;
    strcpy(e[4].cat,"Metal");
    //6th element
        strcpy(e[5].nm,"Potassium");
    strcpy(e[5].blk,"s");
    strcpy(e[5].sym,"K");
    e[5].atmno=19;
    e[5].mass=39.10;
    strcpy(e[5].cat,"Non Metal");
    //7th element
    strcpy(e[6].nm,"Calcium");
    strcpy(e[6].blk,"s");
    strcpy(e[6].sym,"Ca");
    e[6].atmno=20;
    e[6].mass=40.08;
    strcpy(e[6].cat,"Metal");
    //8th elment
        strcpy(e[7].nm,"Rubidium");
    strcpy(e[7].blk,"s");
    strcpy(e[7].sym,"Rb");
    e[7].atmno=37;
    e[7].mass=85.47;
    strcpy(e[7].cat,"Metal");
    //9th element
    strcpy(e[8].nm,"Strontium");
    strcpy(e[8].blk,"s");
    strcpy(e[8].sym,"Sr");
    e[8].atmno=38;
    e[8].mass=87.62;
    strcpy(e[8].cat,"Metal");
    //10th element
    strcpy(e[9].nm,"Scandium");
    strcpy(e[9].blk,"d");
    strcpy(e[9].sym,"Sc");
    e[9].atmno=21;
    e[9].mass=44.96;
    strcpy(e[9].cat,"Metal");
    //11th element
    strcpy(e[10].nm,"Titanium");
    strcpy(e[10].blk,"d");
    strcpy(e[10].sym,"Ti");
    e[10].atmno=22;
    e[10].mass=47.88;
    strcpy(e[10].cat,"Metal");
    //12th element
    strcpy(e[11].nm,"Vanadium");
    strcpy(e[11].blk,"d");
    strcpy(e[11].sym,"V");
    e[11].atmno=23;
    e[11].mass=50.94;
    strcpy(e[11].cat,"Metal");
    //13th element
    strcpy(e[12].nm,"Chromium");
    strcpy(e[12].blk,"d");
    strcpy(e[12].sym,"Cr");
    e[12].atmno=24;
    e[12].mass=52;
    strcpy(e[12].cat,"Metal");
    //14th element
    strcpy(e[13].nm,"Mangnese");
    strcpy(e[13].blk,"d");
    strcpy(e[13].sym,"Mn");
    e[13].atmno=25;
    e[13].mass=54.64;
    strcpy(e[13].cat,"Metal");
    //15th element
    strcpy(e[14].nm,"Iron");
    strcpy(e[14].blk,"d");
    strcpy(e[14].sym,"Fe");
    e[14].atmno=26;
    e[14].mass=56.08;
    strcpy(e[14].cat,"Metal");
    //16th element
    strcpy(e[15].nm,"Cobalt");
    strcpy(e[15].blk,"d");
    strcpy(e[15].sym,"Co");
    e[15].atmno=27;
    e[15].mass=58.93;
    strcpy(e[15].cat,"Metal");
    //17th element
    strcpy(e[16].nm,"Nickel");
    strcpy(e[16].blk,"d");
    strcpy(e[16].sym,"Ni");
    e[16].atmno=28;
    e[16].mass=58.69;
    strcpy(e[16].cat,"Metal");
    //18th element
    strcpy(e[17].nm,"Copper");
    strcpy(e[17].blk,"d");
    strcpy(e[17].sym,"Cu");
    e[17].atmno=29;
    e[17].mass=63.55;
    strcpy(e[17].cat,"Metal");
    //19th element
    strcpy(e[18].nm,"Zinc");
    strcpy(e[18].blk,"d");
    strcpy(e[18].sym,"Zn");
    e[18].atmno=30;
    e[18].mass=65.39;
    strcpy(e[18].cat,"Metal");
    //20th element
    strcpy(e[19].nm,"Palladium");
    strcpy(e[19].blk,"d");
    strcpy(e[19].sym,"Pd");
    e[19].atmno=46;
    e[19].mass=106.44;
    strcpy(e[19].cat,"Metal");
    //21th element
    strcpy(e[20].nm,"Silver");
    strcpy(e[20].blk,"d");
    strcpy(e[20].sym,"Ag");
    e[20].atmno=47;
    e[20].mass=107.9;
    strcpy(e[20].cat,"Metal");
    //22nd element
    strcpy(e[21].nm,"Cadmium");
    strcpy(e[21].blk,"d");
    strcpy(e[21].sym,"Cd");
    e[21].atmno=48;
    e[21].mass=112.4;
    strcpy(e[21].cat,"Metal");
    //23rd element
    strcpy(e[22].nm,"Lanthanium");
    strcpy(e[22].blk,"d");
    strcpy(e[22].sym,"La");
    e[22].atmno=57;
    e[22].mass=138.9;
    strcpy(e[22].cat,"Metal");
    //24th element
    strcpy(e[23].nm,"Boron");
    strcpy(e[23].blk,"p");
    strcpy(e[23].sym,"B");
    e[23].atmno=5;
    e[23].mass=10.81;
    strcpy(e[23].cat,"Metalliod");
    //25th element
    strcpy(e[24].nm,"Carbon");
    strcpy(e[24].blk,"p");
    strcpy(e[24].sym,"C");
    e[24].atmno=6;
    e[24].mass=12.01;
    strcpy(e[24].cat,"Non Metal");
    //26th element
    strcpy(e[25].nm,"Nitrogen");
    strcpy(e[25].blk,"p");
    strcpy(e[25].sym,"N");
    e[25].atmno=7;
    e[25].mass=14.01;
    strcpy(e[25].cat,"Non Metal");
    //27th element
    strcpy(e[26].nm,"Oxygen");
    strcpy(e[26].blk,"p");
    strcpy(e[26].sym,"O");
    e[26].atmno=8;
    e[26].mass=16;
    strcpy(e[26].cat,"Non Metal");
    //28th element
    strcpy(e[27].nm,"Flourine");
    strcpy(e[27].blk,"p");
    strcpy(e[27].sym,"F");
    e[27].atmno=9;
    e[27].mass=19;
    strcpy(e[27].cat,"Non Metal");
    //29th element
    strcpy(e[28].nm,"Neon");
    strcpy(e[28].blk,"p");
    strcpy(e[28].sym,"Ne");
    e[28].atmno=10;
    e[28].mass=20.18;
    strcpy(e[28].cat,"Non Metal");
    //30th element
    strcpy(e[29].nm,"Aluminium");
    strcpy(e[29].blk,"p");
    strcpy(e[29].sym,"Al");
    e[29].atmno=13;
    e[29].mass=26.98;
    strcpy(e[29].cat,"Metal");
    //31st element
    strcpy(e[30].nm,"Silicon");
    strcpy(e[30].blk,"p");
    strcpy(e[30].sym,"Si");
    e[30].atmno=14;
    e[30].mass=28.09;
    strcpy(e[30].cat,"Metalloid");
    //32nd element
    strcpy(e[31].nm,"Phosphorous");
    strcpy(e[31].blk,"p");
    strcpy(e[31].sym,"P");
    e[31].atmno=15;
    e[31].mass=30.97;
    strcpy(e[31].cat,"Non Metal");
    //33rd element
    strcpy(e[32].nm,"Sulphur");
    strcpy(e[32].blk,"p");
    strcpy(e[32].sym,"S");
    e[32].atmno=16;
    e[32].mass=32.06;
    strcpy(e[32].cat,"Non Metal");
    //34th element
    strcpy(e[33].nm,"Chlorine");
    strcpy(e[33].blk,"p");
    strcpy(e[33].sym,"Cl");
    e[33].atmno=17;
    e[33].mass=35.45;
    strcpy(e[33].cat,"non Metal");
    //35th element
    strcpy(e[34].nm,"Argon");
    strcpy(e[34].blk,"p");
    strcpy(e[34].sym,"Ar");
    e[34].atmno=18;
    e[34].mass=39.95;
    strcpy(e[34].cat,"Non Metal");
    //36th element
    strcpy(e[35].nm,"Gallium");
    strcpy(e[35].blk,"p");
    strcpy(e[35].sym,"Ga");
    e[35].atmno=31;
    e[35].mass=69.72;
    strcpy(e[35].cat,"Metal");
    //37th element
    strcpy(e[36].nm,"Germanium");
    strcpy(e[36].blk,"p");
    strcpy(e[36].sym,"Ge");
    e[36].atmno=32;
    e[36].mass=72.64;
    strcpy(e[36].cat,"Metalloid");
    //38th element
    strcpy(e[37].nm,"Arsenic");
    strcpy(e[37].blk,"p");
    strcpy(e[37].sym,"As");
    e[37].atmno=33;
    e[37].mass=74.92;
    strcpy(e[37].cat,"Metalloid");
    //39th element
    strcpy(e[38].nm,"Selenium");
    strcpy(e[38].blk,"p");
    strcpy(e[38].sym,"Se");
    e[38].atmno=34;
    e[38].mass=78.96;
    strcpy(e[38].cat,"Non Metal");
    //40th element
    strcpy(e[39].nm,"Bromin");
    strcpy(e[39].blk,"p");
    strcpy(e[39].sym,"Br");
    e[39].atmno=35;
    e[39].mass=79.90;
    strcpy(e[39].cat,"Non Metal");
    //41st element
    strcpy(e[40].nm,"Krypton");
    strcpy(e[40].blk,"p");
    strcpy(e[40].sym,"Kr");
    e[40].atmno=36;
    e[40].mass=83.79;
    strcpy(e[40].cat,"Non Metal");
    //42nd element
    strcpy(e[41].nm,"Cerium");
    strcpy(e[41].blk,"f");
    strcpy(e[41].sym,"Ce");
    e[41].atmno=88;
    e[41].mass=140.10;
    strcpy(e[41].cat,"Metal");
    //43rd element
    strcpy(e[42].nm,"Thorium");
    strcpy(e[42].blk,"f");
    strcpy(e[42].sym,"Th");
    e[42].atmno=90;
    e[42].mass=232;
    strcpy(e[42].cat,"Metal");
    //44th element
    strcpy(e[43].nm,"Uranium");
    strcpy(e[43].blk,"f");
    strcpy(e[43].sym,"U");
    e[43].atmno=92;
    e[43].mass=238;
    strcpy(e[43].cat,"Metal");
    //45th element
    strcpy(e[44].nm,"Plutonium");
    strcpy(e[44].blk,"f");
    strcpy(e[44].sym,"Pu");
    e[44].atmno=94;
    e[44].mass=244;
    strcpy(e[44].cat,"Metal");


    for(i=0;i<45;i++)
    {

        fprintf(fp,"%s %s %s %d %f %s",e[i].nm,e[i].sym,e[i].blk,e[i].atmno,e[i].mass,e[i].cat);
    }
    fclose(fp);
    home();
    return 0;
}
void home()
{
      int opt;
      printf("\n\t\t***********************PERIODIC TABLE***********************");
      printf("\n\n\n1.SEARCH ELEMENT\n\n2.ADD ELEMENT\n\n3.EXIT\n\nEnter Your Choice:");
      scanf("%d",&opt);
      if(opt<1||opt>3)
      {
        printf("\nInvalid Choice\nEnter Valid Choice:");
        scanf("%d",&opt);
      }
      switch(opt)
      {
        case 1:
            search();
            break;
        case 2:
            add();
            break;

        case 3:
            return ;
      }
}

//here it searches for a particular element in a file
void search()
{
    int i,j,cho,on,flg=0;
    char mn[50];
    float wt;
    printf("\n1.By Name\n\n2.By Atomic No\n\n3.By Atomic Mass\n\n4.Back to Homescreen\n\nEnter Your Choice:");
    scanf("%d",&cho);
    if(cho<1||cho>4)
    {
        printf("\nInvalid Choice\nEnter Correct Choice between 1&4:");
        scanf("%d",&cho);
    }
    switch(cho)
    {
        case 1:
             printf("\nEnter Name Of Element:\n\t\t");
             scanf("%s",mn);

             for(j=0;mn[j]!='\0';j++)
             {
                if(j==0&&(mn[j]<=122&&mn[j]>=97))
                     mn[j]-=32;
                else
                {
                   if(j!=0&&(mn[j]>=65&&mn[j]<=90))
                     mn[j]+=32;
                   else
                      break;                            
                }

             }
              for(i=0;i<tot;i++)
              {
                if(strcmp(mn,e[i].nm)==0)
                {
                    flg=1;
                    printf("\nName:%s \nSymbol:%s \nBlock:%s \nAtomic No:%d \nAtomic Mass:%f \nCategory:%s",e[i].nm,e[i].sym,e[i].blk,e[i].atmno,e[i].mass,e[i].cat);
                }
                else
                {
                    if(flg==0&&i==tot-1)
                    printf("\nNot found");
                }
              } 
            home();
            break;
        case 2:
                printf("\nEnter Atomic No:");
                 scanf("%d",&on);
                for(i=0;i<tot;i++) 
                {
                    if(on==e[i].atmno)
                    {
                        flg=1;
                        printf("\nName:%s \nSymbol:%s \nBlock:%s \nAtomic No:%d \nAtomic Mass:%f \nCategory:%s",e[i].nm,e[i].sym,e[i].blk,e[i].atmno,e[i].mass,e[i].cat);
                    }   
                    else
                    {
                        if(flg==0&&i==tot-1)
                            printf("\nNot found");
                   }
            }
            home();
            break;
        case 3:
                printf("\nEnter Exact Atomic Mass upto 2 decimal places:");
                scanf("%f",&wt);
                 for(i=0;i<tot;i++) 
                 {
                    if(wt==e[i].mass)
                    {
                      flg=1;
                     printf("\nName:%s \nSymbol:%s \nBlock:%s \nAtomic No:%d \nAtomic Mass:%f \nCategory:%s",e[i].nm,e[i].sym,e[i].blk,e[i].atmno,e[i].mass,e[i].cat);
                    }
                    else
                    {
                        if(flg==0&&i==tot-1)
                            printf("\nNot found");
                    }

                }
                home();
                break;
        case 4:
                home();

   } 
}

//Here, is the add function which adds the element in a file
void add()
{
        int n,j;
        FILE *fp1;
        fp1=fopen("per.txt","a");
        if(fp1==NULL)
        {
            printf("\nFailed to open");
        }
        else
        {
            printf("\nEnter Number of Elements You Want to Add:");
            scanf("%d",&n);
            for(i=tot;i<tot+n;i++)
            {
            printf("\nName of element:\n\t\t");
            scanf("%s",e[i].nm);
            for(j=0;e[i].nm[j]!='\0';j++)
             {
                if(j==0&&(e[i].nm[j]<=122&&e[i].nm[j]>=97))
                     e[i].nm[j]-=32;
                else
                {
                   if(j!=0&&(e[i].nm[j]>=65&&e[i].nm[j]<=90))
                     e[i].nm[j]+=32;
                   else
                      break;                            
                }
              }
            printf("\nSymbol:\n");
            scanf("%s",e[i].sym);
            printf("\nBlock:\n");
            scanf("%s",e[i].blk);
            printf("\nAtomic No:\n");
            scanf("%d",&e[i].atmno);
            printf("\nAtomic Weight:\n");
            scanf("%f",&e[i].mass);
            printf("\nType of Element:\n");
            scanf("%s",e[i].cat);   
            fprintf(fp1,"%s %s %s %d %f %s",e[i].nm,e[i].sym,e[i].blk,e[i].atmno,e[i].mass,e[i].cat);
        }
    }
    tot+=n;
    fclose(fp1);
    home();
    return ;        
}

最佳答案

您从未阅读过该文件。当程序启动时,初始化数组并写入文件。添加元素时,您将添加到数组并添加到文件。但旧数据永远不会被检索。

您应该先尝试打开该文件,如果该文件不存在,则写入初始化数据。如果文件有,则不写入初始化数据,直接读取文件,初始化元素数组。

更改写入格式,使数据之间以逗号分隔,这样以后读取数据会更方便。每行应以 "\n"

结尾
char filename[] = "per.txt";
int main()
{
    int i;
    FILE *fin = fopen(filename, "r");

    if(fin == NULL)
    {
        FILE *fout = fopen(filename, "w");
        if(fout)
        {
            if(fout == NULL)
            {
                printf("cannot read/write...\n");
                return 0;
            }

            el x[] =
            {
                { "Hydrogen",  "H",  "ns",  1,  2,      "Non Metal" },
                { "Lithium",   "Li", "s",   3,  6.94f,  "Metal" },
                { "Berillium", "Li", "s",   4,  9.02f,  "Metal" },
                { "Sodium",    "Na", "s",   11, 22.99f, "Metal" },
            };

            for(i = 0; i < 4; i++)
                fprintf(fout, "%s, %s, %s, %d, %f, %s\n", 
                    x[i].nm, x[i].sym, x[i].blk, x[i].atmno, x[i].mass, x[i].cat);
        }
        fclose(fout);
        fin = fopen(filename, "r");
    }

    if(fin == NULL)
    {
        printf("cannot read...\n");
        return 0;
    }

    tot = 0;
    while(fscanf(fin, "%[^,], %[^,], %[^,],%d,%f, %[^\n]\n",
        e[tot].nm, e[tot].sym, e[tot].blk, &e[tot].atmno, &e[tot].mass, e[tot].cat) == 6)
    {
        tot++;
    }

    home();
    return 0;
}

void add()
{
    ...
    fprintf(fp1, "%s, %s, %s, %d, %f, %s\n", 
            e[i].nm, e[i].sym, e[i].blk, e[i].atmno, e[i].mass, e[i].cat);
    ...
    tot += n;
    fclose(fp1);
    home();
    return;
}

关于c - 为什么每次我再次编译程序时都需要在文件中添加相同的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51341869/

相关文章:

c - char 和 int 上的 C struct 位字段之间的区别

c - 我如何在没有图形库的情况下与 C 中的 X 服务器通信?

C struct stat 信息没有在循环中更新,给出了荒谬的修改日期

c - 将简单的字符标记放置在阵列板上以实现简单的糖果粉碎游戏

c++ - 强制将位域读取为 32 位

c - K&R 2-3 htoi 段错误

c - 如何正确打印文件路径?

c - 释放所有由 malloc(), realloc() 在 C 中分配的内存

找不到头文件

c - 将输入读入数组