主题:sizeof为2,strlen为31
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *这段代码把.gjf格式的文件中原子信息拷贝出来,将元素标志存储在数组element中,将原子位置坐标存 *储在数组cart中 *++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 2000 int main(int argc, char *argv[]) { FILE *in; float x,y,z, cart[MAX][3]; char ch, tmp[10], element[MAX][3]; int count, n=0; if((in=fopen(argv[1],"r")) == NULL){ printf("Can't open file!"); exit(1); } /*读文件,并正确获取元素标志以及(x,y,z)坐标*/ while((ch=getc(in)) != EOF) for(count=0; fscanf(in, "%s%f%f%f", tmp, &x, &y, &z) == 4; count++){ element[count][0]=tmp[0]; element[count][1]=tmp[1]; element[count][2]='\0'; //字符串数组需要用'\0'来结尾,否则内容会“越界” cart[count][0]=x; cart[count][1]=y; cart[count][2]=z; } /*识别元素标志*/ for(count=0; element[count][0]!='\0'; count++) printf("%2d\t%2s\t%d\t%d\n",count, element[count], strlen(element[count]), sizeof(element[count])); fclose(in); return 0; }
mts@ubuntu-unity:~/Primer_Plus$ ./test g.gjf 0 u 1 3 1 Cu 2 3 2 Cu 2 3 3 Cu 2 3 4 Cu 2 3 5 Cu 2 3 6 Cu 2 3 7 Cu 2 3 8 Cu 2 3 9 Cu 2 3 10 Cu 2 3 11 Cu 2 3 12 Cu 2 3 13 Cu 2 3 14 Cu 2 3 15 Cu 2 3 16 N 1 3 17 H 1 3 18 H 1 3 19 Tv 2 3 20 Tv 2 3 21 Tv 2 3 *** stack smashing detected ***: ./test terminated 已放弃 (核心已转储)1、首行输出应该为Cu,现在变成了u,怎么修改呢?
2、程序是被异常终止的,这是什么原因,应该如何修改?
谢谢。
这是用到的g.gjf文件
%chk=2-d.chk # pbepbe/3-21g/auto Title Card Required 0 2 Cu 0.00000000 0.00000000 0.00000000 Cu -1.27152577 2.20234546 0.00000000 Cu 2.54305000 0.00000000 0.00000000 Cu 1.27152423 2.20234546 0.00000000 Cu 0.00000000 0.00000000 6.25786859 Cu -1.27152577 2.20234546 6.25786859 Cu 2.54305000 0.00000000 6.25786859 Cu 1.27152423 2.20234546 6.25786859 Cu 1.27152474 0.73411515 4.17254272 Cu -0.00000103 2.93646061 4.17254272 Cu 3.81457474 0.73411515 4.17254272 Cu 2.54304897 2.93646061 4.17254272 Cu -0.00000051 1.46823031 2.08301468 Cu -1.27152628 3.67057577 2.08301468 Cu 2.54304949 1.46823031 2.08301468 Cu 1.27152372 3.67057577 2.08301468 N 0.52295394 1.19172963 7.72778695 H 1.32628647 1.22326721 8.37113887 H -0.28259955 1.24167882 8.36798725 Tv 5.08610000 0.00000000 0.00000000 Tv -2.54305154 4.40469092 0.00000000 Tv 0.00000000 0.00000000 21.01084000