Linux C 编程一站式学习 第25章 C标准库 综合练习二题解 文件格式转换
来源:互联网 发布:java正则表达式语法 编辑:程序博客网 时间:2024/05/21 10:05
/**************************************************
test.ini file is:
;Configuration of http
[http]
domain=www.mysite.com
port=8080
cgihome=/cgi-bin
;Configuration of db
[database]
server = mysql
user = myname
password = toopendatabase
After conveter test.xml file is:
<!-- 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>
***************************************************/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int isEmptyLine(char *str);
char* delStartLastSpace(char *str);
void change_Ini2Xml(char *srcfile, char *destfile)
{
FILE *srcfp = NULL;
FILE *destfp = NULL;
char buf[81];
char *str = NULL;
char *head = NULL;
char headbuf[80];
int condition = 1;
int index = 0;
int ch = 0;
srcfp = fopen(srcfile, "r");
if (srcfp == NULL)
{
printf("Open the source file error!\n");
exit(1);
}
destfp = fopen(destfile, "w");
if (destfp == NULL)
{
printf("Open the destination file error!\n");
exit(1);
}
while (condition)
{
ch = fgetc(srcfp);
if (EOF == ch) /* if read the end of the file set condition to 0 */
{
condition = 0;
}
buf[index++] = ch;
if (('\n' == ch) || ('\0' == ch) || (EOF == ch)) /* have read a line */
{
buf[--index] = '\0';
index = 0;
if (isEmptyLine(buf)) /* check whither the line is empty? */
{
if (head != NULL)
{
fprintf(destfp, "</%s>\n", head);
head = NULL;
}
fprintf(destfp, "\n");
}
else
{
switch (buf[0])
{
case ';':
str = strtok(buf, ";");
fprintf(destfp, "<!-- %s-->\n", str);/* string has a 回车 at last */
break;
case '[':
str = strtok(buf + 1, "]");
strcpy(headbuf, str);
head = headbuf;
fprintf(destfp, "<%s>\n", str);
break;
default:
str = strtok(buf, "="); /* get the section name */
str = delStartLastSpace(str); /* delete the start and end space of the string */
fprintf(destfp, "\t\t<%s>", str);
str = strtok(NULL, "=");
str = delStartLastSpace(str);
while (*str != '\0') /* the last line of file end with '\0' */
{
/* the line isn't the last line of the file end with 回车,it's asscii code is 0x0D */
if (*str != 0x0D) /* if the char is not 回车,write to destfp */
{
fputc(*str, destfp);
}
str++;
}
str = strtok(buf, "=");
str = delStartLastSpace(str);
fprintf(destfp, "</%s>\n", str);
break;
}
}
}
}
printf("%s--->%s\nThe convert success!\n", srcfile, destfile);
if (head != NULL)
{
fprintf(destfp, "</%s>\n", head);/* write the end of the section name */
}
fclose(srcfp);
fclose(destfp);
}
/*******************************************************
return 0: the string is not empty.
return 1: the string is empty.
empty string is contain zero or many space characters
and tab characters.
note:
the line isn't the last line of the file end with 回车,
it's asscii code is 0x0D.
********************************************************/
int isEmptyLine(char *str)
{
while (*str != '\0')
{
//printf("%c %x\n", *str, *str);
if ((*str != ' ') && (*str != '\t') && (*str != 0x0D) && (*str != '\n'))
{
return 0;
}
str++;
}
return 1;
}
char* delStartLastSpace(char *str)
{
char *temp = NULL;
int len = 0, i = 0;
temp = str;
while (temp[i] != '\0')
{
if ((' ' == temp[i]) || ('\t' == temp[i])) /* delelte the start space */
{
i++;
}
else
{
temp[len++] = temp[i++];
}
}
--len;
while ((' ' == str[len]) || ('\t' == str[len]) )/* delelte the last space */
{
--len;
}
str[++len] = '\0';
return str;
}
int main(void)
{
change_Ini2Xml("test.ini", "test.xml");
return 0;
}
- Linux C 编程一站式学习 第25章 C标准库 综合练习二题解 文件格式转换
- c语言综合练习二
- C语言编程入门——综合练习(二)
- 【C++】学习笔记二十——第5章编程练习
- linux c 多线程编程练习(二)
- 《 Linux C编程一站式学习》连载二
- linux C 编程 一站式学习(二)
- Linux C IO编程学习笔记(第一天:标准IO)
- [C/C++]标准MIDI文件格式
- C语言综合练习
- 一站式学习C编程第5章
- 【C++Primer学习笔记】第3章 标准库类型
- C语言编程练习(二)
- C语言编程练习二
- Linux C 多线程编程学习(二)
- C语言编程入门——综合练习(一)
- 学习c编程练习1
- 【C++】学习笔记十四——C++ primer plus第六版第4章编程练习
- 《Linux那些事儿之我是USB》我是U盘(24)彼岸花的传说(三)
- java propertyChangeListener 观察者模式的实现
- the given port name does not start with COM/com异常解决办法
- 在visual studio 2008下编译C程序
- unix环境高级编程-mkdir和rmdir函数
- Linux C 编程一站式学习 第25章 C标准库 综合练习二题解 文件格式转换
- 【观光旅游】解题报告
- 开源游戏通讯引擎RakNet
- POJ3160 Father Christmas flymouse 强连通tarjan算法+dfs+简单dp
- POJ 3696 The Luckiest number
- 【XEN学习笔记】XEN添加和调用Hypercall
- Linux驱动编程 step-by-step (一)
- 验证码的操作与实现
- 数据量非常大的照片分享社区网站架构设计