gpt4 book ai didi

c - 数据进/出循环的差异

转载 作者:太空宇宙 更新时间:2023-11-04 02:57:33 25 4
gpt4 key购买 nike

我正在尝试编写一个读取 .pdb 文件的程序,该文件是生物学应用中使用的一种文件类型。这种类型的文件具有标准格式,数据之间有不同的空白。该文件的形式是

ATOM      4  N   ALA     1       2.670   1.801   1.072  0.00  0.00
ATOM 5 CA ALA 1 3.225 3.144 1.197 0.00 0.00
ATOM 6 C ALA 1 4.408 3.341 0.256 0.00 0.00
ATOM 7 O ALA 1 4.553 4.394 -0.363 0.00 0.00
.... . .. ... . ..... ..... ..... ..... ....

所以我的程序(可能写得不好)定义了一个结构,读取数据(我从此处的另一篇文章中窃取的 http://www.daniweb.com/software-development/c/threads/65455/reading-a-file-using-fscanf# ),并将其存储到索引结构中。现在,如果我打印内部 if 循环内的值,它会吐出正确的数据。但是,当我在外部 while 循环外打印出相同的值时,它给了我错误的 atom[].name(恰好是 HA,输入文件中第三列数据中的最后一个值。所有其他值是正确的。

这是我的程序

#include <stdio.h>

typedef struct
{
char *atm;
int serial;
char *name;
char *resName;
int resSeq;
double x;
double y;
double z;
double occupancy;
double tempFactor;
} pdb;

int main(int argc, char** argv)
{
int i, j;
pdb atom[28];
char atm[5];
char name[3];
char resName[4];
int serial;
int resSeq;
double x;
double y;
double z;
double occupancy;
double tempFactor;
char buff[BUFSIZ];
FILE *file = fopen("test.pdb", "r");

i = 0;
while (fgets(buff, sizeof buff, file) != NULL)
{
if (sscanf(buff, "%s %d %s %s %d %lf %lf %lf %lf %lf",
atm, &serial, name, resName, &resSeq, &x, &y, &z,
&occupancy, &tempFactor) == 10)
{
atom[i].atm = atm;
atom[i].serial = serial;
atom[i].name = name;
atom[i].resName = resName;
atom[i].resSeq = resSeq;
atom[i].x = x;
atom[i].y = y;
atom[i].z = z;
atom[i].occupancy = occupancy;
atom[i].tempFactor = tempFactor;
i++;
/*printf("%s ", atom[i].atm);
printf("%d ", atom[i].serial);
printf("%s ", atom[i].name);
printf("%s ", atom[i].resName);
printf("%d ", atom[i].resSeq);
printf("%lf ", atom[i].x);
printf("%lf ", atom[i].y);
printf("%lf ", atom[i].z);
printf("%lf ", atom[i].occupancy);
printf("%lf\n", atom[i].tempFactor);*/
}
}
fclose(file);
for (j = 0; j < i; j++)
printf("%d of %d: %s\n", j, i-1, atom[j].name);

return(0);
}

知道为什么会这样吗?此外,对于程序格式/结构的任何帮助也将不胜感激。我更喜欢 Fortran,所以 C 结构超出了我的专业领域。

提前致谢。

编辑: jsn 帮我解决了问题,Randy Howard 对其进行了改进。这是更新后的工作程序:

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
char *atm;
int serial;
char *name;
char *resName;
int resSeq;
double x;
double y;
double z;
double occupancy;
double tempFactor;
} pdb;

int main(int argc, char** argv)
{
int i, j;
pdb atom[28];
int serial;
int resSeq;
double x;
double y;
double z;
double occupancy;
double tempFactor;
char buff[BUFSIZ];
FILE *file = fopen("test.pdb", "r");

i = 0;
while (fgets(buff, sizeof buff, file) != NULL)
{
char *atm = malloc(sizeof(char) * 5);
char *name = malloc(sizeof(char) * 3);
char *resName = malloc(sizeof(char) * 4);

if (sscanf(buff, "%s %d %s %s %d %lf %lf %lf %lf %lf",
atm, &serial, name, resName, &resSeq, &x, &y, &z,
&occupancy, &tempFactor) == 10)
{
atom[i].atm = atm;
atom[i].serial = serial;
atom[i].name = name;
atom[i].resName = resName;
atom[i].resSeq = resSeq;
atom[i].x = x;
atom[i].y = y;
atom[i].z = z;
atom[i].occupancy = occupancy;
atom[i].tempFactor = tempFactor;
i++;
}
}
fclose(file);
for (j = 0; j < i; j++)
{
printf("%s ", atom[j].atm);
printf("%d ", atom[j].serial);
printf("%s ", atom[j].name);
printf("%s ", atom[j].resName);
printf("%d ", atom[j].resSeq);
printf("%lf ", atom[j].x);
printf("%lf ", atom[j].y);
printf("%lf ", atom[j].z);
printf("%lf ", atom[j].occupancy);
printf("%lf\n", atom[j].tempFactor);
}

return(0);
}

最佳答案

在 while 循环中,您需要为每个名称的每个 char* 分配新内存。您现在正在覆盖它们。

while (fgets(buff, sizeof buff, file) != NULL) 
{

char *atm = malloc(sizeof(char) * 5);
char *name = malloc(sizeof(char) * 3);
char *resName = malloc(sizeof(char) * 4);

if (sscanf(buff, "%s %d %s %s %d %lf %lf %lf %lf %lf",
atm, &serial, name, resName, &resSeq, &x, &y, &z,
&occupancy, &tempFactor) == 10)

您正在复制 char 数组(指针),因此所有名称都应该相同(最后一个条目)。

关于c - 数据进/出循环的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15766673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com