XTC文件读取错误

时间:2017-02-10 07:34:18

标签: c unix gcc structure bioinformatics

#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;
}

我尝试使用xtc库运行代码来读取GROMACS的轨迹框架...我收到错误,

Segmentation error
你能帮忙吗?

1 个答案:

答案 0 :(得分:2)

查看this code

必须将第二个参数nat设置为检索到的read_xtc_natoms函数值。无论如何,必须初始化该值。

此外,数组k宽度必须与nat值匹配。

#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;
}

在上面的代码中,我用VLAs来创建k数组,但你也可以使用动态内存:

#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;
}