问题
#include "xdrfile/xdrfile_xtc.h"
#include "xdrfile/xdrfile.h"
#include<stdio.h>
int main()
{
int nat;
int step;
float time;
float prec;
int status;
matrix box;
rvec k[3];
XDRFILE* xfp=xdrfile_open("test2.xtc","r");
status=read_xtc(xfp,nat,&step,&time,box,k,&prec);
xdrfile_close(xfp);
return 0;
}
I tried to run the code using the xtc library to read a trajectory frame of GROMACS... I am getting an error,
Segmentation error
Can you please help???
回答1:
Looking at this code
Second parameter nat
must be set to a value retrieved read_xtc_natoms
function. The value must be initialized anyway.
Moreover the array k
width must match the nat
value.
#include "xdrfile/xdrfile_xtc.h"
#include "xdrfile/xdrfile.h"
#include<stdio.h>
int main(void)
{
int nat;
int step;
float time;
float prec;
int status;
matrix box;
int status;
char fileName[] = "test2.xtc";
status = read_xtc_natoms(fileName, &nat);
if (status != exdrOK)
{
XDRFILE* xfp = xdrfile_open(fileName, "r");
if (xfp != NULL)
{
rvec k[nat];
status = read_xtc(xfp, nat, &step, &time, box, k, &prec);
xdrfile_close(xfp);
}
else
{
perror("File not opened:");
}
}
else
{
fprintf(stderr, "read_xtc_natoms failure; return code %d", status);
}
return 0;
}
In the above code I used VLAs to create the k array, but you can use dynamic memory too:
#include "xdrfile/xdrfile_xtc.h"
#include "xdrfile/xdrfile.h"
#include<stdio.h>
int main(void)
{
int nat;
int step;
float time;
float prec;
int status;
matrix box;
int status;
char fileName[] = "test2.xtc";
status = read_xtc_natoms(fileName, &nat);
if (status != exdrOK)
{
rvec *k = malloc(nat * sizeof(rvec));
if (k != NULL)
{
XDRFILE* xfp = xdrfile_open(fileName, "r");
if (xfp != NULL)
{
status = read_xtc(xfp, nat, &step, &time, box, k, &prec);
xdrfile_close(xfp);
}
else
{
perror("File not opened:");
}
free(k);
}
else
{
fprintf(stderr, "Error in dynamic allocation of k vector\n");
}
}
else
{
fprintf(stderr, "read_xtc_natoms failure; return code %d", status);
}
return 0;
}
来源:https://stackoverflow.com/questions/42153868/xtc-file-reading-error