#include
#include
#include
/**
*Split line into fields
*gcc 005.c -o 006.app `pkg-config --cflags --libs glib-2.0`
*/
int split(char* line,char* pattern);
int main(int argc,char** argv){
FILE* fp = fopen("./006.dat","r");
if(fp == NULL){
g_print("%s\n",strerror(errno));
return -1;
}
char line[1024];
int line_no = 0;
while(fgets(line,1024,fp)){
g_strstrip(line);
split(line,"\\s+");
}
fclose(fp);
return 0;
}
int split(char* line,char* pattern){
GRegex* regex = NULL;
GMatchInfo* match_info = NULL;
guint i = 0;
regex = g_regex_new(pattern,0,0,NULL);
gchar** fields = g_regex_split(regex,line,0);
guint len = g_strv_length(fields);
for(i = 0; i < len;i++){
g_print("(%s)",fields[i]);
}
g_print("\n");
g_strfreev(fields);
g_regex_unref(regex);
}
数据文件006.dat
/dev/sda9 15G 8.8G 4.8G 65% /
none 495M 240K 495M 1% /dev
none 501M 516K 500M 1% /dev/shm
none 501M 96K 501M 1% /var/run
none 501M 0 501M 0% /var/lock
阅读(741) | 评论(0) | 转发(0) |