0

I have a file that contains 80 characters per line. I want to go to a particular line that starts with "ATOM".

I tried with fscanf(f1," %s%*[^\n]", rec) and compare rec with strcmp(rec,"ATOM"), but it reads the next line from the match.
I also tried with fscanf("line_format", variables), but this reads somewhere else from the file.

The line is

ATOM      1  N   MET A   1      36.643 -24.862   8.890  1.00 24.11             N 

From this I want to read character by character and assign it to variables. I have a problem with the float values and spaces. If I find a space in a place of particular variable how do I read it? How do I read the float values if there is no space between them?

4

2 回答 2

1

您可以使用 读取输入文件中的每一行,fgets()使用strtok()或对其进行标记,将第一个标记与“ATOM”进行比较,然后使用或strtok_r()解析其余标记,如有必要,将它们转换为浮点数或整数。atof()atoi()

虽然这有点矫枉过正,因为ATOMPDB 文件中的记录具有定义明确的结构,具有固定大小的字段,并且任何符合要求的 pdb 文件都更容易解析。您只需选择相关的子字符串并将它们传递给atof()or atoi()

于 2012-06-19T08:42:47.490 回答
0

我相信您的 (未显示) 中有错误line_format。你真的应该能够做到:

if( fscanf(f1, "ATOM %d %s %s %s %d %f %f %f %f %f %s", /* ... */) == 11 )
{
  /* store/analyze/print the parsed values */
}

当然请注意,这会冒覆盖字符串参数的风险。您可以使用更具体的格式字符串来限制长度。

于 2012-06-19T08:54:44.350 回答