回复 6楼 grmmylbs
我按照您上次的知道又写了一个文件对data文件处理,
ITEM: TIMESTEP
10000
ITEM: NUMBER OF ENTRIES
2088
ITEM: BOX BOUNDS pp pp pp
-5 25
-5 25
-2 52
ITEM: ENTRIES index c_1[1] c_1[2] c_1[3] c_2[1]c_2[2]
主要是看每组当中的第四行每次增加量,但是每次运行到最后一组时就会出现debug错误,不知道是怎么回事,希望您能帮忙看下,万分感谢
#include
#include
FILE *pfile;
typedef struct
{
int id;
int atom1;
int atom2;
int type;
float x;
float y;
} Data;
int main()
{
Data tmp[50]; //定义一个临时变量,存储每一行数据
Data *result; //这个数组长度要与文件行数相一致,不能小于
int n, i = 1, count,number, sum=0; float S = 0, m, x1, y1, z1 ;
int chains,ret;
FILE *infile;
infile = fopen("bond.dump", "r");
if (infile == NULL)
{
printf("\nFailed to open the file");
exit(1);
}
//注意分配内存时 数字的大小对应ID大小
for (i = 0; i < 9; i++)
{
ret = fgets((char *)tmp, sizeof(tmp), infile); //跳过文字行
if(i==3)
{
sscanf((char *)tmp, "%d", &(number)); //放到数组中
}
}
result = malloc(number * sizeof(Data));
while (ret)
{
memset((void *)result,0, number * sizeof(Data));
i = 1;
while (fgets((char *)tmp, sizeof(tmp), infile)) //读每一行
{
sscanf((char *)tmp, "%d %d %d %d %f %f", &(result[i].id), &(result[i].atom1), &(result[i].atom2), &(result[i].type), &(result[i].x),&(result[i].y)); //放到数组中
printf("%d %d
%d %d
%d
%f
%f\n", i, result[i].id, result[i].atom1, result[i].atom2,result[i].type, result[i].x, result[i].y); //测试显示到屏幕上
if (i == number)
{
sum=number-2088;
//
printf("%d\n",sum);
if((pfile=fopen("output.data","a"))==NULL)
printf("lFile could not be opened.\n");
else
{
fprintf(pfile,"%d\n", (number-2088) );
}
fclose(pfile);
for (i = 0; i < 9; i++)
{
ret = fgets((char *)tmp, sizeof(tmp), infile); //跳过非数值行
if(i==3)
{
sscanf((char *)tmp, "%d", &(number)); //放到数组中
printf("%d\n",number);
}
}
break;
}
i++;
}
}
free(result);
fclose(infile);
system("pause");
return 0;
}