Linux C编程一站式学习 标准I/O库函数 ini转xml

Linux C编程一站式学习 25章中第二节标准I/O库函数,最后的练习

输入 my.ini

;Configuration of http
[http]
domain=www.mysite.com
port=8080
cgihome=/cgi-bin

;Configuration of db
[database]
server = mysql
user = myname
password = toopendatabase

输出 my.xml

<!-- Configuration of http -->
<http>
	<domain>www.mysite.com</domain>
	<port>8080</port>
	<cgihome>/cgi-bin</cgihome>
</http>
<!-- Configuration of db -->
<database>
	<server>mysql</server>
	<user>myname</user>
	<password>toopendatabase</password>
</database>

参考这篇文章,这篇文章中提到的下面这问题,原因是如果读取到最后一个字符后,最后一个字符是EOF,不满足\n条件,会进入死循环,直到程序崩溃。
在这里插入图片描述
在条件里增加判断(ch = fgetc(ini_fp)) != '\n' && ch != EOF就可以了。

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#define BUFF_SIZE BUFSIZ
#define NOTE_FORMAT "<!-- %s -->"
#define START_XML_FORMAT  "<%s>"
#define END_XML_FORMAT "</%s>"


FILE *target_fp;
char *errmsg;
char *ini_file_name = "my.ini";
char *xml_file_name = "my.xml";

void write_xml(char *buf){
    fputs(buf, target_fp);
}
void process_notes(char *buf, void (*write_xml)(char *)){
    char *note = malloc(sizeof(char) * (strlen(buf)));
    sprintf(note,NOTE_FORMAT,buf);
    write_xml(note);
    free(note);
}
void process_start_xml(char *sec, void (*write_xml)(char *)){
    char *section = malloc(sizeof(char) * (strlen(sec)));
    sprintf(section,START_XML_FORMAT,sec);
    write_xml(section);
    free(section);
}
void process_end_xml(char *sec, void (*write_xml)(char *)){
    char *section = malloc(sizeof(char) * (strlen(sec)));
    sprintf(section,END_XML_FORMAT,sec);
    write_xml(section);
    free(section);
}

int main(int argc, char **argv) {

    FILE *ini_fp, *xml_fp;

    if ((ini_fp = fopen(ini_file_name, "r+")) == NULL) {
        sprintf(errmsg, "file %s not exits", ini_file_name);
        goto close_label;
    }

    if ((xml_fp = fopen(xml_file_name, "w+")) == NULL) {
        sprintf(errmsg, "create file %s failure", xml_file_name);
        goto close_label;
    }
    char ch = '\0';
    char *section= malloc(sizeof(char) * BUFF_SIZE);
    char *line= malloc(sizeof(char) * BUFF_SIZE);

    char *k,*v;
    int i = 0;

    target_fp = xml_fp;

    void (*f)(char *) = write_xml;

    while ((ch = fgetc(ini_fp)) != EOF) {
        if (ch == ';') {
            //note
            if (strlen(section) > 1) {
                process_end_xml(section, f);
                f("\n");
            }

            i = 0;
            memset(line,0,BUFF_SIZE);
            while ((ch = fgetc(ini_fp)) != '\n') {
                line[i++] = ch;
            }
            process_notes(line, f);
            f("\n");
            continue;
        }
        if (ch == '[') {
            //section
            memset(section, 0, BUFF_SIZE);
            i = 0;
            while ((ch = fgetc(ini_fp)) != ']') {
                section[i++] = ch;
            }
            process_start_xml(section, f);
            f("\n");
            continue;
        }
        if (ch == '\n' || ch == '\t' || ch == ' ') {
            continue;
        }
        //key value
        memset(line,0,BUFF_SIZE);
        i = 0;
        line[i++] = ch;
        while ((ch = fgetc(ini_fp)) != '\n' && ch != EOF) {
            if (ch == ' ' || ch == '\t') {
                continue;
            }
            line[i++] = ch;
        }
        k = strtok(line, "=");
        v = strtok(NULL, "=");
        f("\t");
        process_start_xml(k, f);
        f(v);
        process_end_xml(k, f);
        f("\n");
    }

    process_end_xml(section, f);
    close_label:
    if (xml_fp != NULL) {
        fclose(xml_fp);
    }
    if (ini_fp != NULL) {
        fclose(ini_fp);
    }
    if (errmsg != NULL) {
        goto err;
    }

    free(section);
    free(line);
    return 0;

    err:
    printf("errno: %d,%s\n", errno, errmsg);
    free(errmsg);
    exit(1);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

起个破名真费劲..

可赏可不赏,没脸要,哈哈

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值