请教C语言如何遍历文本文件,比较取得其中想要的内容?(高分悬赏)

RT,这是从一个软件里输出的一个后缀名为CLS的文件,但是可以直接用文本软件打开的。现在想用C语言遍历这个文件的GOTO标志后面的第三个数值,然后进行比较,取得里面的最小值,请问各位大师,这个代码应该怎么写?谢谢了!附上云盘链接:http://pan.baidu.com/s/1eQpGSLk
这个是输出的CLS文件,望各位高手出手帮帮小弟,谢谢了 http://pan.baidu.com/s/1eQpGSLk

/*===========CLS file process===========*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>

const double INF = 1e5;
const double EPS = 1e-8;

char str[1000];

int main()
{
    double ret = INF, tmp;
    freopen("cls-2.cls","r",stdin);//cls和cpp置于同一目录下
    while(gets(str)!=NULL){
        if(strstr(str,"GOTO/")){
            int len = strlen(str);
            int flg = 2, i;
            for(i=0; i<len && flg; i++){
                if(str[i]==',') flg--;
            }
            for(int j=i;j<=len;j++) str[j-i] = str[j];
            tmp = atof(str);
            //printf("Get data = %.4f\n",tmp);
            if(tmp < ret) ret = tmp;
        }
    }
    printf("******************************\n");
    if(fabs(ret-INF)<EPS) printf("Found no \"GOTO/\" data!\n");
    else printf("The minimum data is %.4f\n",ret);
    return 0;
}

C语言版。。。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-08-23
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
FILE *pf;
char pl[513],*pw;
char sp[]="/,";
int i;
double min=1e30, d;
pf=fopen("cls-2.cls","r");
if(!pf){
printf("打开文件时出错\n");
return 1;
}
while(fgets(pl,512,pf)!=0){
strlwr(pl); //将字符串pl中的字母变为小写
pw=strtok(pl,sp); //提前字符串pl中的首个单词
if(strcmp(pw,"goto")!=0) continue;
for(i=0; i<3; i++)
pw=strtok(0,sp); //提取GOTO之后的第三个数字
d=atof(pw);
if(d<min) min=d;
}
printf("min=%.6lf\n",min);
fclose(pf);
return 0;
}追问

谢谢这位兄弟,可惜只能给一个人加分,真心谢谢你

第2个回答  2014-08-23
#include<iostream>
#include<cstring>
#include<fstream>
using namespace std;
double n,minn=0;
char s[5],o;
int main()
{
fstream file("cls-2.cls",ios::in|ios::binary);
while(!file.eof())
{
o=file.get();
if(o=='G')
{
s[0]=file.get();
s[1]=file.get();
s[2]=file.get();
s[3]=file.get();
s[4]='\0';
if(!strcmp(s,"OTO/"))
{
file>>n>>o>>n>>o>>n;
if(n<minn||minn==0) minn=n;
}
}
}
file.close();
cout<<minn;
}

c++的可以吗?

追问

谢谢这位兄弟,可惜只能给一个人加分,真心谢谢你

第3个回答  2014-08-23
#include <fstream>
#include <iostream>
using namespace std;
int main(){
ifstream file;
file.open("cls-2.cls");
double num,minn;
num=minn=0;
bool first=true;
char ch;
const char text[6]="GOTO/";
int i;
while(!file.eof()){
for(i=0;i<5;i++){
file.get(ch);
if(ch!=text[i]) break;
}
if(5==i){
file>>num>>ch>>num>>ch>>num;
if(first){
    minn=num;first=false;
}
else if(num<minn) minn=num;
}
}
cout<<minn<<endl;
return 0;
}

追问

谢谢这位兄弟,可惜只能给一个人加分,真心谢谢你

相似回答