- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我可以在元素周期表中添加新元素,并将其添加到文件中。但问题是,当我下次编译后搜索新插入的元素时,但它说找不到特定元素在一个文件中。为什么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/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!