我收到的错误与标题相同。我认为它正在发生,因为矩阵A但无法理解为什么以及如何解决它。
请帮助我。这是完整的代码。我正在使用g ++来编译代码。
#include <stdio.h>
#include <fstream>
#include <math.h>
#include <string.h>
using namespace std;
void count()
{
FILE *file, *output, *list;
file = fopen("coordinate", "r");
output = fopen("number", "w");
list = fopen("list", "w");
int min;
printf("Enter the minimum number of atoms for critical clusters :");
scanf("%d",&min);
int i, j, count=0, cnt=0, fin_count=0, id;
int nmonomer= 165;
double x, y, z, sumx, sumy, sumz, sum=0;
char dump[1000];
while (fgets(dump, 1000, file)!=NULL)
{
if (strlen(dump)==1)
count++;
}
fclose(file);
printf("%d\n",count);
file = fopen("coordinate", "r");
int clus_cnt[count];
for(i=0; i<count; i++)
clus_cnt[i] = 0;
while (fgets(dump, 1000, file)!=NULL)
{
if (strlen(dump)==1)
{
fprintf(list, "%5d%5d\n", (cnt+1), clus_cnt[cnt]);
cnt++;
}
else
{
fprintf(list, "%s", dump);
clus_cnt[cnt]++;
}
}
fclose(file);
fclose(list);
file = fopen("list", "r");
for (i=0; i<count; i++)
{
if (clus_cnt[i] >= min)
{
sumx=0;
sumy=0;
sumz=0;
for (j=0; j<clus_cnt[i]; j++)
{
fscanf(file, "%lf%lf%lf\n", &x, &y, &z);
sumx += x;
sumy += y;
sumz += z;
}
//fprintf (output, "%8d%8d%12.3f%12.3f%12.3f\n", i+1, clus_cnt[i], sumx, sumy, sumz);
fprintf (output, "%5d%5d%8.3f%8.3f%8.3f\n", i+1, clus_cnt[i], sumx/(double)clus_cnt[i], sumy/(double)clus_cnt[i], sumz/(double)clus_cnt[i]);
fgets(dump, 1000, file);
fin_count++;
}
else
{
for (j=0; j<clus_cnt[i]+1; j++)
fgets(dump, 1000, file);
}
}
printf("%d\n", fin_count);
fclose(file);
fclose(output);
file = fopen("number", "r");
for (i=0; i<fin_count; i++)
{
fscanf(file, "%d%d%lf%lf%lf\n", &id, &cnt, &x, &y, &z);
sum += cnt;
}
sum /= (double)fin_count;
printf ("The Average Number of Clusters :%8.3f\n", sum);
fclose(file);
}
int main()
{
FILE *file, *output, *coords, *selection;
file = fopen("dump.sti", "r");
output = fopen("cluster_list", "w");
coords = fopen("coordinate", "w");
double cutoff;
printf("Enter the cutoff distance :");
scanf("%lf",&cutoff);
int i, natom=8122, nmonomer=165, nstep=9, id, type, index=0, j, lk, mol, k, sel, s;
double xs, ys, zs, xi, yi, zi, x[nmonomer], y[nmonomer], z[nmonomer], xj, yj, zj, vx, vy, vz, dist, rxk, ryk, rzk, q;
int l[nmonomer], ind[nmonomer], flag[nmonomer];
char dump[1000];
int A[8122];
for (i=0; i<8122; i++)
{ fscanf(file, "%d%d%lf%lf%lf%lf%lf%lf\n", &id, &type, &xs, &ys, &zs, &xi, &yi, &zi);
A[i] = type;
}
fclose(file);
for (s=0; s<natom; s++)
{
selection = fopen("selection_fxfg", "r");
fscanf(file, "%d%d%lf%lf%lf%lf%lf%lf\n", &id, &type, &xs, &ys, &zs, &xi, &yi, &zi);
for (i=0; i<nmonomer; i++)
{
fscanf(selection, "%d", &sel);
if (id == sel)
{
x[index] = xs + (46.1206 + 40.1206)*xi;
y[index] = ys + (86.8457 + 56.8457)*yi;
z[index] = zs + (35.9228 + 35.9228)*zi;
ind[index] = id;
index++;
}
}
fclose(selection);
}
printf("%d\n", index);
//-------------
for (i=0; i<nmonomer; i++)
l[i] = i;
for (i=0; i<nmonomer-1; i++)
{
if (i == l[i])
{
j=i;
xj = x[j];
yj = y[j];
zj = z[j];
for (k=i+1; k<nmonomer; k++)
{
lk = l[k];
if (lk == k)
{
rxk = xj-x[k];
ryk = yj-y[k];
ryk = yj-y[k];
rzk = zj-z[k];
dist = sqrt(pow(rxk,2)+pow(ryk,2)+pow(rzk,2));
if (dist <= cutoff)
{
l[k] = l[j];
l[j] = lk;
}
}
}
j = l[j];
xj = x[j];
yj = y[j];
zj = z[j];
while (j != i)
{
for (k=i+1; k<nmonomer; k++)
{
lk = l[k];
if (lk == k)
{
rxk = xj-x[k];
ryk = yj-y[k];
rzk = zj-z[k];
dist = sqrt(pow(rxk,2)+pow(ryk,2)+pow(rzk,2));
if (dist <= cutoff)
{
l[k] = l[j];
l[j] = lk;
}
}
}
j = l[j];
xj = x[j];
yj = y[j];
zj = z[j];
}
}
}
//-------------
for (i=0; i<nmonomer; i++)
{
flag[i] = 0;
fprintf(output, "%8d%8d\n", i+1, l[i]+1);
}
int lit;
for (i=0; i<nmonomer; i++)
{
if (flag[i] == 0)
{
lit = l[i];
if (lit != i)
{
fprintf(coords, "%10.3f %10.3f %10.3f%d\n", x[i], y[i], z[i],A[i]);
while (lit != i)
{
flag[lit] = 1;
fprintf(coords, "%10.3f %10.3f %10.3f%d\n", x[lit], y[lit], z[lit], A[lit]);
lit = l[lit];
}
fprintf(coords, "\n");
}
else
fprintf(coords, "%10.3f %10.3f %10.3f%d\n\n", x[i], y[i], z[i], A[i]);
}
}
fclose(file);
fclose(output);
fclose(coords);
count();
}
提前多多感谢。
答案 0 :(得分:1)
你在主要中间打电话给fclose(file)
,但继续从中读取并在主要结束时再次关闭它。
对同一文件句柄进行两次fclose调用可能会导致双重自由错误(或者可能在另一次运行中完全执行其他操作)。
答案 1 :(得分:1)
删除第102行
fclose(file);