"Exercises" program to convert INI files into XML files

Source: Internet
Author: User
Tags open ini file strtok

; Configuration of Http[http]domain=www.mysite.comport=8080cgihome=/cgi-bin; Configuration of db[database]server = Mysqluser = Mynamepassword = Toopendatabase

A configuration file consists of a number of sections, and the section name is enclosed by [] parentheses. Under each section there are several key = value pairs (key-value pair), which can have 0 or more whitespace characters (spaces or tab) on either side of the equals sign, one row for each key-value pair. A line that begins with a number is a comment. Each section ends with one or more empty rows, which are lines that contain only 0 or more white space characters (spaces or tabs). INI file may or may not have a line break after the last line.

<!--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] = {'};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 Characterwhile (buf[i] = = ' \ t ' | | buf[i] = = ") {i++;continue;}  ch = strchr (buf+i, ' \ n '); Remove line break if (ch = NULL) *ch = ' &buf[i+1 '; switch (Buf[i]) {case '; ': fprintf (out, "<!--%s-->\n", 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);} memset (temp, ' Break;default:key ', sizeof (temp)); fprintf (out, "\ n"); fprintf (stdout, "\ n"); 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;}} if (feof (in) && strlen (temp)! = 0)//If the end of the file is written to the parent node </>{fprintf (out, "</%s>", temp); fprintf (stdout, " </%s>\n ", temp);} Fclose (in); fclose (out); return 0;}





"Exercises" program to convert INI files into XML files

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.