#include "so_test.h" int main() { FILE *fp1,*fp2; char str[50]; char name[20]; char value[20]; char *ps,*p,*py,*pl,*pr; struct cfg *head,*ph,*q1,*q2; if( ( fp1=fopen("ggg.txt","r") )==NULL) { printf("fail open file!!!"); } if( ( fp2=fopen("ddd.txt","w+") )==NULL) { printf("fail open file!!!"); } /*read data from fp1 (config_file) to a new file fp2 */ while(!feof(fp1)) { fgets(str,50,fp1); ps=str; /*截断行首无关字符*/ while( *ps == ' ') { ps++; } p=strchr(str,'='); pl=p-1; pr=p+1; while( (*ps != '#')&&( *ps != '/0')&& ( *ps != '/n')) { /*截断等号左边无关字符*/ while( *pl == ' ') { pl--; } *(pl+1)='/0'; /*读取等号右边关键字的值字符串的首地址(py)*/ while( *pr== ' ') { *pr++; } py=pr; /*截断右过无关字符*/ while( (*pr!=' ') && (*pr!='/n') && (*pr!='/0') ) { pr++; } *pr='/0'; /*生成新的配置文件*/ fprintf(fp2,"%s =%s/n",ps,py); break; } continue; } /*transer the date to link talbe from fp2*/ rewind(fp2); ph=head=q1=(struct cfg *)malloc(sizeof(struct cfg)); fscanf(fp2,"%s =%s/n",q1->name,q1->value); while(!feof(fp2)) { q2=q1; q1=(struct cfg *)malloc(SIZE); fscanf(fp2,"%s =%s/n",q1->name,q1->value); q2->next=q1; } q1->next=NULL; fclose(fp1); fclose(fp2); /*read the link table*/ while(ph!=NULL) { printf("%s =%s/n",ph->name,ph->value); ph=ph->next; } return 0;
so_test.h :
#include <stdio.h> #include <string.h> #include <malloc.h> #include <stdlib.h> //#include <mysql.h> #define filename "yyc" #define SIZE sizeof(struct cfg) #define SQL_QUERY "select * from student where age=%d" FILE *crcf(); struct cfg* red_cfg(); struct cfg { char name[20]; char value[20]; struct cfg *next; };
