Linux C 编程一站式学习 第25章 C标准库 综合练习二题解 文件格式转换
2011-10-26 20:52
726 查看
/**************************************************
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;
}
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 编程一站式学习--Unix Domain Socket IPC 练习代码
- 【深度学习】【caffe实用工具3】笔记25 Windows下caffe中将图像数据集合转换为DB(LMDB/LEVELDB)文件格式之convert_imageset
- 《linux c 编程一站式学习》课后部分习题解答
- Python学习1(环境搭建、文件格式与简单编程)
- 基于java的图片文件格式转换和线性缩放-Java基础-Java-编程开发
- 《linux c 编程一站式学习》课后部分习题解答
- Linux C 编程一站式学习
- linux C 编程 一站式学习(二)
- 《linux c 编程一站式学习》课后部分习题解答(一)
- 导出文本、表格、图像到PDF格式文件中(学习整理)
- 【编程练习】将读入的每个单词转换为大写
- YUV格式学习:YUV420P、YV12、NV12、NV21格式转换成RGB24
- Lee 4000 tCode编程练习 - Reverse String学习心得
- 关于怎么将PDF文件转换成PPT格式的方法
- 检测到 Mac 文件格式: 请将源文件转换为 DOS 格式或 UNIX 格式
- 如何把PDF文件转换成PPT格式的步骤
- Ubuntu下的wxWidgets编程(学生信息管理写入文件,文件格式是.txt)
- Python实现把json格式转换成文本或sql文件
- 【嵌入式Linux学习七步曲之第五篇 Linux内核及驱动编程】揭开Linux Proc文件系统的神秘面纱
- java 文件格式转换工具