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);
}