首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

c语言读取altair.txt资料

2012-10-12 
c语言读取altair.txt文件文本内容如下:Feature: hypeworks version: 11.0 vendor: altairstart date: 2012

c语言读取altair.txt文件
文本内容如下:

Feature: hypeworks version: 11.0 vendor: altair
start date: 2012-09-07 Expire date: 2012-09-20
30 of 100000 license used:
30 license used by root@aifserver[10.109.40.53]
login time: 2012-09-19 12:26 checkout time: 2012-09-19 11:26


这是文件的一段,还有许多相同格式的段落内容,我想用C语言逐行读取":"后边的内容,最好是用正则表达式去匹配
例如:定义一个char[256]="Feature: .*";通过定义这样的字符数组去匹配文档,如果有就分别读取":"后边的内容,还有就是读取 “30 of 100000 license used:”这段字符中的数值,还有“30 license used by root@aifserver[10.109.40.53]”这段字符中的@后边的aifserver和[10.109.40.53], 各位大侠帮帮忙吧,小弟C语言不是太懂,如果可以代码写的详细点,急!!!!

[解决办法]

C/C++ code
//in.txt//Feature: hypeworks version: 11.0 vendor: altair//start date: 2012-09-07 Expire date: 2012-09-20#include <stdio.h>FILE *f;char ln[1000];char s[1000];int n;char *p;int L,k;int main() {    f=fopen("in.txt","r");    if (NULL!=f) {        n=0;        while (1) {            if (NULL==fgets(ln,1000,f)) break;            n++;            printf("Line %d\n",n);            p=ln;            while (1) {                k=sscanf(p,"%*[^:]: %[^ \n]%n",s,&L);                if (k==1) {                    printf("  the string after \': \' is [%s]\n",s);                    p+=L;                } else if (k==-1) {                    break;                } else {                    p++;                }            }        }        fclose(f);    }    return 0;}//Line 1//  the string after ': ' is [hypeworks]//  the string after ': ' is [11.0]//  the string after ': ' is [altair]//Line 2//  the string after ': ' is [2012-09-07]//  the string after ': ' is [2012-09-20] 

热点排行