[英]How can fscanf(), in C, be used to read a .gro file?
I am trying to read the following gro file via a C code.我正在尝试通过 C 代码读取以下 gro 文件。
FJP in Pol Water in water t= 0.00000 step= 0
16
1FJP P 1 5.346 7.418 0.319
2FJP P 2 5.151 7.405 0.499
3FJP P 3 5.260 7.178 0.428
4FJP P 4 5.159 6.961 0.342
5FJP P 5 5.355 6.909 0.220
6FJP P 6 5.169 6.824 0.043
7FJP P 7 5.068 6.669 11.454
8FJP P 8 4.919 6.861 11.482
9FJP P 9 4.835 7.075 11.364
10FJP P 10 4.738 6.987 11.197
11FJP P 11 4.847 7.115 10.993
12FJP P 12 4.642 7.126 10.870
13FJP P 13 4.680 6.940 10.674
14FJP P 14 4.521 7.052 10.545
15FJP P 15 4.321 6.973 10.513
16FJP P 16 4.315 6.728 10.516
11.56681 11.56681 11.56681
My code is:我的代码是:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[])
{
char input_file[]="file.gro";
FILE *input;
char *myfile=malloc(sizeof(char)*80);
sprintf(myfile,"%s",input_file); //the .gro file being read in
input=fopen(myfile,"r");
double dummy1,dummy6,dummy7,dummy8,dummy9,dummy10,dummy11;
int dummy2,dummy3,dummy4,dummy5;
int lines=0;
while (fscanf(input,"FJP in Pol Water in water t= %lf step= %d",&dummy1,&dummy2)==2
||fscanf(input," %d\n",&dummy3)==1
||fscanf(input," %dFJP P %d %lf %lf %lf\n",
&dummy4,&dummy5,&dummy6,&dummy7,&dummy8)==5
||fscanf(input," %lf %lf %lf\n",&dummy9,&dummy10,&dummy11)==3)
{
printf("%lf %d\n",dummy1,dummy2);
printf("%d\n",dummy3);
printf("%d %d\n",dummy4,dummy5);
printf("%lf %lf %lf\n",dummy6,dummy7,dummy8);
printf("%lf %lf %lf\n",dummy9,dummy10,dummy11);
lines=lines+1;
}
printf("lines=%d\n",lines);
fclose(input);
}
The problem is the values printed by the various dummy variables do not match what is in the file.问题是各种虚拟变量打印的值与文件中的值不匹配。 Also, the number of lines being read is 3 as opposed to 19, which matches the file.此外,读取的行数是 3,而不是 19,这与文件匹配。 I am not certain what is incorrect about my fscanf() statements to read this file.我不确定我的 fscanf() 语句读取这个文件有什么不正确。 Any help for this problem would be much appreciated.对这个问题的任何帮助将不胜感激。
Your main problem is that you are assuming scanf is better than it is.您的主要问题是您假设 scanf 比它更好。
Scanf will read and parse as many arguments as it can, and then give up. Scanf 将尽可能多地读取和解析参数,然后放弃。 It does not rewind to the start of the scanf.它不会倒回到 scanf 的开头。 Also it treats spaces and newlines (and tabs) as simply "skip all whitespace"它还将空格和换行符(和制表符)视为简单的“跳过所有空格”
So the line printf("%d\\n",dummy3)
will try to parse the main lines, eg 1FJP
因此, printf("%d\\n",dummy3)
行将尝试解析主行,例如1FJP
It will read the digit 1 OK into dummy3, but then get stuck because P != a whitespace.它会将数字 1 OK 读入 dummy3,但随后会卡住,因为 P != 一个空格。
All the other rules will then get stuck, because none of them expect a P or any string first.然后所有其他规则都将卡住,因为它们都不会首先期望 P 或任何字符串。
If you want to do it this way, you will just have to apply the scanf statements more intelligently as and when they are expected.如果您想这样做,您只需在预期时更智能地应用 scanf 语句。
The problem is that you try to read and match the header repeatedly, before each line read (in the while loop.) you should read the head once, then read the lines.问题是您尝试重复读取和匹配标题,在读取每一行之前(在 while 循环中)。您应该读取一次标题,然后读取行。 You also only need to skip any given piece of whitespace once.您也只需要跳过任何给定的空格一次。 So you end up with code like:所以你最终得到如下代码:
if (fscanf(input,"FJP in Pol Water in water t=%lf step=%d%d", &dummy1, &dummy2, &dummy3) != 3) {
fprintf(stderr, "Invalid header\n");
exit(1); }
while (fscanf(input,"%dFJP P%d%lf%lf%lf", &dummy4, &dummy5, &dummy6, &dummy7, &dummy8) == 5) {
... read a line of the table
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.