"Exercise" Programming the INI file into an XML file __ programming

Source: Internet
Author: User
Tags open ini file strlen strtok

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

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

A configuration file consists of several section names, which are enclosed in [] brackets. There are several key = value pairs (key-value Pair) below each section, with 0 or more white space characters (spaces or tabs) on either side of the equals sign, and one row for each key value pair. A line that starts with a number is a comment. At the end of each section, there is one or more blank lines, which are rows containing only 0 or more white space characters (spaces or tabs). The last line of the INI file may or may not be followed by a newline character.

<!--Configuration of HTTP-->

Program:
#include <stdio.h> #include <stdlib.h> #include <string.h> int main (int argc, char *argv[]) {FILE *in
	, *out;
	Char buf[1024];
	Char temp[1024] = {' I '};
	Char *key, *value;
	Char *ch;

	int i;
		if (ARGC < 3) {printf ("Usage:name.ini name.xml\n");
	Exit (1);
	} in = fopen (Argv[1], "R");
		if (in = = NULL) {perror ("Open INI file");
	Exit (1);
	out = fopen (argv[2], "w+");
		if (out = = NULL) {perror ("Open XML File");
	Exit (1);
		while (Fgets (buf,sizeof (BUF), in)!= NULL) {i = 0;
			Skip blank character while (buf[i] = = ' t ' | | | buf[i] = = ') {i++;
		Continue  ch = strchr (buf+i, ' \ n ');

		Remove newline character if (ch!= NULL) *ch = ' yes ';
			Switch (Buf[i]) {case '; ': fprintf (out, <!--%s-->\n, &buf[i+1]);
			fprintf (stdout, "<!--%s-->\n", &buf[i+1]);
		Break
			Case ' [': ch = strchr (buf+i, '] ');
			if (ch!= NULL) *ch = ';
			fprintf (out, "<%s>\n", &buf[i+1]); fprintf (stdout, "<%s>\n", &buf[i+1]);
			strcpy (temp,&buf[i+1]);
		Break
				Case ' strlen '://Empty line if (temp)!= 0) {fprintf (out, "</%s>\n", temp);
			fprintf (stdout, "</%s>\n", temp);
			(temp, ' memset ', sizeof (temp));
			fprintf (out, "\ n");
			fprintf (stdout, "\ n");
		Break
			Default:key = Strtok (&buf[i], "=");
			Value = Strtok (NULL, "=");
			fprintf (out, "\t<%s>%s</%s>\n", Key,value,key);
			fprintf (stdout, "\t<%s>%s</%s>\n", Key,value,key);
		Break }
	}
<span style= "White-space:pre" >	<span style= "Font-family:courier new;font-size:18px;color: #008000; o Rphans:2; Widows:2; " ><span style= "FONT-SIZE:15PT;" ></span></span>//If you are at the end of a file, write the last Tag</></span>,<span style= "White-space:pre" to the parent node >strlen (temp)!= 0 excludes the last line of line breaks when redundant </> writes to <span style= "ORPHANS:2; Widows:2; font-size:15pt; Color:rgb (0, 128, 0); font-family: ' Courier New '; ></span></span>
	if (feof (in) && strlen (temp)!= 0)  
	{
		fprintf (out, "</%s>", temp);
		fprintf (stdout, "</%s>\n", temp);
	}
	Fclose (in);
	Fclose (out);

	return 0;
}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.