zoukankan      html  css  js  c++  java
  • 一个将配置文件转换成xml的示例程序

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    int main()
    {
    char*p;
    char buf1[256];
    char buf2[256];
    char *c;
    int len=0;
    FILE*fp1,*fp2;
    fp1=fopen("z.txt","r");
    fp2=fopen("x.xml","w");
    while(fgets(buf1,256,fp1)!=NULL)
    {
    if(buf1[0]=='#')
    {
    len=strlen(buf1);
    buf1[len-1]='\0';

    buf1[len-1]='\0';
    fprintf(fp2,"<!-----%s----->\n",buf1+1);
    }
    else if(buf1[0]=='!')
    {
    len=strlen(buf1);
    buf1[len-1]='\0';
    strcpy(buf2,buf1);
    fprintf(fp2,"<%s>\n",buf1+1);
    }
    else if(buf1[0]=='\n')
    {
    fprintf(fp2,"<%s>\n",buf2+1);
    }
    else
    {
    len=strlen(buf1);
    buf1[len-1]='\0';
    p=buf1;
    c=strsep(&p,"=");
    fprintf(fp2,"<%s>",c);
    fflush(fp2);
    fprintf(fp2,"%s",p);
    fflush(fp2);
    fprintf(fp2,"</%s>\n",c);
    }
    }
    return 0;

    }

    z.txt如下:

    [y@YMY C]$ cat z.txt
    #config of network
    !network
    ip=192.168.11.6
    port=8000
    home-path=/home/admin/

    #config of database
    !database
    server=mySQL
    user=admin
    password=133432

    运行结果:

    [y@YMY C]$ gcc b.c
    [y@YMY C]$ ./a.out
    [y@YMY C]$ cat x.xml
    <!-----config of network----->
    <network>
    <ip>192.168.11.6</ip>
    <port>8000</port>
    <home-path>/home/admin/</home-path>
    <network>
    <!-----config of database----->
    <database>
    <server>mySQL</server>
    <user>admin</user>
    <password>133432</password>
    <database>
    [y@YMY C]$

  • 相关阅读:
    Go语言专题
    计算机网络专题
    分布式系统理论专题
    Scala语言专题
    Zookeeper专题
    Java虚拟机专题
    Java并发编程专题
    git使用指南
    Oracle查询今天、昨天、本周、上周、本月、上月数据
    python3 装饰器
  • 原文地址:https://www.cnblogs.com/ymy124/p/2362705.html
Copyright © 2011-2022 走看看