我想将第6列从文本文件“1PGB.pdb”打印到输出文件“out”中。以下是我的代码:
#include <stdio.h>
#include <stdlib.h> /* For exit() function*/
#define ROW 500
#define COL 12
int main()
{
int n6, garbage;
FILE *fp;
FILE *op = fopen("out", "w");
int score[ROW][COL];
int row, col, item, i, j;
if ((fp=fopen("1PGB.pdb","r"))==NULL)
{
printf("Error! opening file");
exit(1); /* Program exits if file pointer returns NULL. */
}
for(row=0; row<ROW; row++)
{
col=5;
fscanf(fp, "%d", &score[row][col]);
fprintf(op, "%d", score[row][col]);
fclose(fp);
fclose(op);
}
return 0;
}
它会出现以下错误。
<。>`./Code1'出错:双重释放或损坏(出):0x0000000000cde250 *** 中止(核心倾销)1PGB.pdb文件的前几行是:
ATOM 1 N MET A 1 12.969 18.506 30.954 1.00 15.93 N
ATOM 2 CA MET A 1 13.935 18.529 29.843 1.00 17.40 C
ATOM 3 C MET A 1 13.138 18.692 28.517 1.00 14.65 C
ATOM 4 O MET A 1 12.007 18.222 28.397 1.00 13.04 O
ATOM 5 CB MET A 1 14.733 17.216 29.882 1.00 20.72 C
ATOM 6 CG MET A 1 15.742 16.983 28.738 1.00 23.81 C
ATOM 7 SD MET A 1 17.378 17.025 29.359 1.00 28.11 S
ATOM 8 CE MET A 1 17.166 16.055 30.819 1.00 27.51 C
ATOM 9 N THR A 2 13.719 19.413 27.573 1.00 12.63 N
ATOM 10 CA THR A 2 13.088 19.661 26.283 1.00 12.68 C
ATOM 11 C THR A 2 13.561 18.631 25.300 1.00 12.02 C
ATOM 12 O THR A 2 14.763 18.432 25.121 1.00 13.07 O
ATOM 13 CB THR A 2 13.527 20.980 25.667 1.00 14.62 C
答案 0 :(得分:2)
这似乎是一种好的,简单的方法:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char line[1000];
int n6;
FILE *fp = fopen("1PGB.pdb", "r");
FILE *op = fopen("out", "w");
if (fp == NULL || op == NULL)
{
fprintf(stderr, "Error opening file.");
exit(1);
}
while (fgets(line, sizeof(line), fp) != 0)
{
/* Use "%*s" to skip a word when using scanf. */
sscanf(line, "%*s %*s %*s %*s %*s %d", &n6);
fprintf(op, "%d\n", n6);
}
return 0;
}