1 /* 2 * FILE: p670_convert.c 3 * DATE: 20180125 4 * ============== 5 * DESCRIPTION: Convert .ini configuration file to .xml file 6 * fgets, fputs, DEBUG(format, ...) debugging, strtok cuts strings, 7 */ 8 9 #include <stdio.h> 10 #include <string.h> 11 12 #define DEBUG(format, ...) printf("FILE: "__FILE__", LINE: %d: "format"\n", __LINE__, ##__VA_ARGS__) 13 #define BUFFSIZE 64 14 15 int main(int argc, char *argv[]) 16 { 17 FILE *fp_src, *fp_dest; 18 char buf[BUFFSIZE]; 19 char buf_out[BUFFSIZE]; 20 char head[16]; 21 char filename[16]; 22 int len; 23 24 if(argc != 2) 25 DEBUG("Usage: ./buld filename"); 26 27 // Verify that the file extension is .ini configuration file 28 len = strlen (argv [1]); 29 if(strcmp(argv[1]+len-3, "ini") != 0) 30 DEBUG("source file format error"); 31 32 fp_src = fopen(argv[1], "r"); // fopen configuration file 33 34 // Change the extension of the .ini configuration file to .xml 35 strcpy(filename, argv[1]); 36 strcpy(&filename[len-3], "xml"); 37 38 // fopen .xml file, create it if it does not exist 39 fp_dest = fopen(filename, "w"); 40 // read each line sequentially 41 while(fgets(buf, BUFFSIZE, fp_src) != NULL) 42 { 43 len = strlen(buf); 44 buf[len-1] = '\0'; 45 // Configuration header 46 if(buf[0] == '#') 47 sprintf(buf_out, "<!-- %s -->\n", buf); 48 else if(buf[0] == '!') 49 { 50 sprintf(buf_out, "<%s>\n", buf+1); 51 strcpy(head, buf+1); // make a copy for later output</head> 52 } 53 else if(buf[0] == '\0') 54 sprintf(buf_out, "</%s>\n\n", head); 55 else 56 { // strtok cuts the string and returns the first address 57 char *p = strtok(buf, "="); 58 // When strtok is called for the first time, the first address of the string needs to be passed in, and then NULL is passed in 59 p = strtok(NULL, "="); 60 sprintf(buf_out, "\t<%s>%s</%s>\n", buf, p, buf); 61 } 62 fputs(buf_out, fp_dest); // fputs 输出 63 } 64 fclose(fp_src); // fclose closes the file stream 65 fclose(fp_dest); 66 return 0; 67 }
Convert .ini configuration file to .xml file format
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326700453&siteId=291194637
Recommended
Ranking