在这个社区的帮助下,我设法编写了一个代码,用于从我需要的输入 xvg 文件中提取一个片段。它几乎对我有用,但它与 strncmp 有一个小问题。我想得到这个输入文件文件的一部分,比如 1000 到 2000,行以 @ 和 # 开头,所以我将这些值存储到名为starting_point和stopping_point.Problem的字符串中是它不识别起始点和停止点。一些随机输出被存储到输出文件中。
它适用于小型输入文件。
但不适用于显示的输入文件。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXS 256
int main (void)
{
char line[MAXS] = {0};
char s1[MAXS];
int length1,length2;
char starting_point[MAXS]="1000";
char stopping_point[MAXS]="2000";
FILE *fs;
fs=fopen("md_new.xvg","w");
while (fgets (line, MAXS, stdin) != NULL) {
char *p = line;
size_t len = strlen (line);
while (len > 0 && (line[len-1] == '\n' || line[len-1] == '\r'))
line[--len] = 0; /* strip newline or carriage rtn */
length1 = strlen(starting_point);
length2 = strlen(stopping_point);
/* storing lines beginning with '#' or '@' or blank lines */
**if (*p == '#' || *p == '@' || !*p)
{
fprintf (fs,"%s\n", line);
printf("%s\n",line);
}
if (strncmp (p,starting_point, length1) == 0)
{
fprintf (fs,"%s\n", line);
printf("%s\n",line);
if (strncmp (p,stopping_point, length2) == 0)
break;
}
}**
return 0;
}
输入文件看起来像这样
#
#mdrun is part of G R O M A C S:
#
#Go Rough, Oppose Many Angry Chinese Serial killers
#
@ title "dH/d\xl\f{}, \xD\f{}H"
@ xaxis label "Time (ps)"
@ s0 legend "dH/d\xl\f{} \xl\f{} 0.1"
@ s1 legend "\xD\f{}H \xl\f{} 0.05"
@ s2 legend "\xD\f{}H \xl\f{} 0.15"
0.0000 -33.8598 1.71168 -1.66746
0.2000 -34.3949 1.73192 -1.702
0.4000 -31.8213 1.61262 -1.56193
0.6000 -32.3563 1.63639 -1.59224
0.8000 -33.6158 1.69898 -1.65539
1.0000 -32.5242 1.65055 -1.59363
1.2000 -33.7464 1.70708 -1.6607
1.4000 -33.0552 1.68563 -1.60985
1.6000 -32.9946 1.66834 -1.62445
1.8000 -31.6345 1.60933 -1.54529
2.0000 -33.1246 1.67736 -1.62769
2.2000 -33.9822 1.71743 -1.67394
2.4000 -32.4887 1.64732 -1.59384
2.6000 -30.0927 1.5349 -1.46508
so on till 100000.000