我需要一个执行以下操作的程序: - 读取文件中的一行 - 暂时存放它们 - 从该块中提取特定列并存储该列 - 继续下一个街区
在bash中看起来像这样:
counter=126
while [ $counter -lt 1344 ]; #1344 because the file has 1344 lines
do
head -n $counter best.sim.simulate > temp1 #read the first 126 lines
blabla #do something with temp1
Unc=$(grep PX temp2 | awk '{print $5}') #extract column 5 of first 126 lines
span=$(grep span temp2 | awk '{print $8}') #extract column 8 of first 126 lines
echo $span $Unc >> results #store the columns
let counter=counter+122 #extend the block
done
在c中,我的尝试看起来像这样:
#include <stdio.h>
int main()
{
FILE *filepointer;
char string[100];
filepointer=fopen("best.sim.simulate", "r");
while (fgets(string, sizeof(string), filepointer) != NULL) {
printf("%s",string); /* print the string */
}
fclose(filepointer); /* close the file */
return 0;
}
我需要做些什么来使c代码与bash代码一样? 顺便说一下:我需要c程序,因为这段代码是一个运行几个小时的更大循环的一部分。在c中,循环运行得更快,就像在bash中一样。
修改 该文件看起来像这样
fake.rf 1440.00000000 61277.45134800286753318 1.20000 7
fake.rf 1440.00000000 61283.88791346459694154 1.20000 7
fake.rf 1440.00000000 61289.34046454497647005 1.20000 7
fake.rf 1440.00000000 61296.15558287300736495 1.20000 7
fake.rf 1440.00000000 61302.19514871358752472 1.20000 7
fake.rf 1440.00000000 61307.28928664621331635 1.20000 7
and so on