#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#include<dir.h>
#define TABLEBEGINSIGN "<table_data name="
#define FIELDBEGINSIGN "<field name="
#define ROWBEGINSIGN "<row>"
#define TABLEENDSIGN "</table_data>"
#define FIELDENDSIGN "</field>"
#define ROWENDSIGN "</row>"
#define MDCORIGINALXML "table.xml"
#define MDCDATAFOLDER "MDCDataBase"
using namespace std;
/*********************************************************
作用:从一字符串中获取两字符之间的子字符串,不包括首尾字符
参数: source,目标字符串; strSon,子字符串; a,起始字符; b,结尾字符;
返回值:子字符串长度
*********************************************************/
int getStringBetweenTwoChar(const char* source,char* strSon,char a,char b)
{
int i,j;
for(i=0;i<strlen(source);i++)//判断目标字符串中是否存在 b 字符
{
if(source[i]==b) break;
}
if(i==strlen(source)) return 0;
for(i=0;i<strlen(source);i++)//判断目标字符串中是否存在 a 字符
{
if(source[i]==a) break;
}
if(i==strlen(source)) return 0;
i++;
for(j=0;(source[i]!=b)&&(source[i]!=0);j++,i++)
{
strSon[j] = source[i];
}
strSon[j]=0;
return j;
}
/*********************************************************
作用:从输入字符串中查找固定字段
参数: source,目标字符串;destination,子字符串;
返回值:查找到返回true,否则返回false
*********************************************************/
bool findString(const char *source,const char *destination)
{
const char *pD=destination;
const char *pS;
for(int i=0;i<strlen(source);i++)
{
if(source[i] == destination[0])
{
pS=&(source[i]);
while(*pS++==*pD++)
{
if(*pD==0)
return true;
}
source++;
}
}
return false;
}
/*********************************************************
作用:创建一张数据表
参数:aRowChar,从xml文件中读取的一行; tableName,创建的数据表的名称
返回值:创建成功,true; 创建失败,false;
*********************************************************/
bool createDataTable(char *aRowChar,char *tableName,FILE **pfp)
{
if(findString(aRowChar,TABLEBEGINSIGN))//查找数据表
{
if(getStringBetweenTwoChar(aRowChar,tableName,'"','"'))//获取数据表名称
{
char fileName[100];
sprintf(fileName,".\\%s\\%s.txt",MDCDATAFOLDER,tableName);
cout << "创建数据表:" << fileName << endl;
FILE *fpTable = fopen(fileName,"w");
if(fpTable == NULL)
{
cout << "创建数据表失败:" << tableName << endl;
return false;
}
fclose(fpTable);
fpTable = fopen(fileName,"a");
if(fpTable == NULL)
{
cout << "打开数据表失败:" << tableName << endl;
return false;
}
*pfp = fpTable;
return true;
}
}
return false;
}
/*********************************************************
作用:将字符串写入到文件中
参数:fpTable,指向写入文件的指针; tableData,需要写入的字符串;
返回值:写入成功,true; 写入失败,false;
*********************************************************/
bool writeDataToTable(FILE *fpTable,const char *tableData)
{
if(fputs(tableData,fpTable))
{
cout << "写数据失败:" << tableData << endl;
return false;
}
cout << tableData;
if(strcmp(tableData,"\n"))
{
fputc(' ',fpTable);
cout << " ";
}
return true;
}
***********************************************************************************
#include "analysis.h"
using namespace std;
int main()
{
FILE *fp = fopen(MDCORIGINALXML,"r");//打开文件
if(fp == NULL)
{
cout << "没有打开文件:" << MDCORIGINALXML << endl;
return 0;
}
char folderName[20] = ".\\MDCDataBase";
cout << "create folder MDCDataBase: " << mkdir(folderName) << endl;
FILE *fpTable;
char aRowChar[100];//每一行字符
char tableName[32];//数据表的名称
char fieldName[32];//表内字段名称
char tableData[32];//表中数据
string strTemp;
strTemp.clear();
while(1)
{
fgets(aRowChar,100,fp);
if(createDataTable(aRowChar,tableName,&fpTable))
{
while(1)
//for(int i=0;i<40;i++)
{
fgets(aRowChar,100,fp);
if(findString(aRowChar,FIELDBEGINSIGN))//"<field name="
{
getStringBetweenTwoChar(aRowChar,fieldName,'"','"');//字段
getStringBetweenTwoChar(aRowChar,tableData,'>','<');//数据
strTemp = strTemp + tableData + ' ';
writeDataToTable(fpTable,fieldName);
}
else if(findString(aRowChar,ROWENDSIGN))//</row>
{
writeDataToTable(fpTable,"\n");
break;
}
else if(findString(aRowChar,TABLEENDSIGN))//"</table_data>"
{
fclose(fpTable);
break;
}
}
writeDataToTable(fpTable,strTemp.c_str());
strTemp.clear();
writeDataToTable(fpTable,"\n");
fgets(aRowChar,100,fp);
if(!findString(aRowChar,ROWBEGINSIGN))//<row>
{
continue;
}
while(1)
//for(int i=0;i<40;i++)
{
fgets(aRowChar,100,fp);
if(findString(aRowChar,FIELDBEGINSIGN))//"<field name="
{
getStringBetweenTwoChar(aRowChar,tableData,'>','<');//数据
writeDataToTable(fpTable,tableData);
}
else if(findString(aRowChar,ROWENDSIGN))//</row>
{
writeDataToTable(fpTable,"\n");
}
else if(findString(aRowChar,TABLEENDSIGN))//"</table_data>"
{
fclose(fpTable);
break;
}
}
}
if(feof(fp) != 0) break;
}
fclose(fp);
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#include<dir.h>
#define TABLEBEGINSIGN "<table_data name="
#define FIELDBEGINSIGN "<field name="
#define ROWBEGINSIGN "<row>"
#define TABLEENDSIGN "</table_data>"
#define FIELDENDSIGN "</field>"
#define ROWENDSIGN "</row>"
#define MDCORIGINALXML "table.xml"
#define MDCDATAFOLDER "MDCDataBase"
using namespace std;
/*********************************************************
作用:从一字符串中获取两字符之间的子字符串,不包括首尾字符
参数: source,目标字符串; strSon,子字符串; a,起始字符; b,结尾字符;
返回值:子字符串长度
*********************************************************/
int getStringBetweenTwoChar(const char* source,char* strSon,char a,char b)
{
int i,j;
for(i=0;i<strlen(source);i++)//判断目标字符串中是否存在 b 字符
{
if(source[i]==b) break;
}
if(i==strlen(source)) return 0;
for(i=0;i<strlen(source);i++)//判断目标字符串中是否存在 a 字符
{
if(source[i]==a) break;
}
if(i==strlen(source)) return 0;
i++;
for(j=0;(source[i]!=b)&&(source[i]!=0);j++,i++)
{
strSon[j] = source[i];
}
strSon[j]=0;
return j;
}
/*********************************************************
作用:从输入字符串中查找固定字段
参数: source,目标字符串;destination,子字符串;
返回值:查找到返回true,否则返回false
*********************************************************/
bool findString(const char *source,const char *destination)
{
const char *pD=destination;
const char *pS;
for(int i=0;i<strlen(source);i++)
{
if(source[i] == destination[0])
{
pS=&(source[i]);
while(*pS++==*pD++)
{
if(*pD==0)
return true;
}
source++;
}
}
return false;
}
/*********************************************************
作用:创建一张数据表
参数:aRowChar,从xml文件中读取的一行; tableName,创建的数据表的名称
返回值:创建成功,true; 创建失败,false;
*********************************************************/
bool createDataTable(char *aRowChar,char *tableName,FILE **pfp)
{
if(findString(aRowChar,TABLEBEGINSIGN))//查找数据表
{
if(getStringBetweenTwoChar(aRowChar,tableName,'"','"'))//获取数据表名称
{
char fileName[100];
sprintf(fileName,".\\%s\\%s.txt",MDCDATAFOLDER,tableName);
cout << "创建数据表:" << fileName << endl;
FILE *fpTable = fopen(fileName,"w");
if(fpTable == NULL)
{
cout << "创建数据表失败:" << tableName << endl;
return false;
}
fclose(fpTable);
fpTable = fopen(fileName,"a");
if(fpTable == NULL)
{
cout << "打开数据表失败:" << tableName << endl;
return false;
}
*pfp = fpTable;
return true;
}
}
return false;
}
/*********************************************************
作用:将字符串写入到文件中
参数:fpTable,指向写入文件的指针; tableData,需要写入的字符串;
返回值:写入成功,true; 写入失败,false;
*********************************************************/
bool writeDataToTable(FILE *fpTable,const char *tableData)
{
if(fputs(tableData,fpTable))
{
cout << "写数据失败:" << tableData << endl;
return false;
}
cout << tableData;
if(strcmp(tableData,"\n"))
{
fputc(' ',fpTable);
cout << " ";
}
return true;
}
***********************************************************************************
#include "analysis.h"
using namespace std;
int main()
{
FILE *fp = fopen(MDCORIGINALXML,"r");//打开文件
if(fp == NULL)
{
cout << "没有打开文件:" << MDCORIGINALXML << endl;
return 0;
}
char folderName[20] = ".\\MDCDataBase";
cout << "create folder MDCDataBase: " << mkdir(folderName) << endl;
FILE *fpTable;
char aRowChar[100];//每一行字符
char tableName[32];//数据表的名称
char fieldName[32];//表内字段名称
char tableData[32];//表中数据
string strTemp;
strTemp.clear();
while(1)
{
fgets(aRowChar,100,fp);
if(createDataTable(aRowChar,tableName,&fpTable))
{
while(1)
//for(int i=0;i<40;i++)
{
fgets(aRowChar,100,fp);
if(findString(aRowChar,FIELDBEGINSIGN))//"<field name="
{
getStringBetweenTwoChar(aRowChar,fieldName,'"','"');//字段
getStringBetweenTwoChar(aRowChar,tableData,'>','<');//数据
strTemp = strTemp + tableData + ' ';
writeDataToTable(fpTable,fieldName);
}
else if(findString(aRowChar,ROWENDSIGN))//</row>
{
writeDataToTable(fpTable,"\n");
break;
}
else if(findString(aRowChar,TABLEENDSIGN))//"</table_data>"
{
fclose(fpTable);
break;
}
}
writeDataToTable(fpTable,strTemp.c_str());
strTemp.clear();
writeDataToTable(fpTable,"\n");
fgets(aRowChar,100,fp);
if(!findString(aRowChar,ROWBEGINSIGN))//<row>
{
continue;
}
while(1)
//for(int i=0;i<40;i++)
{
fgets(aRowChar,100,fp);
if(findString(aRowChar,FIELDBEGINSIGN))//"<field name="
{
getStringBetweenTwoChar(aRowChar,tableData,'>','<');//数据
writeDataToTable(fpTable,tableData);
}
else if(findString(aRowChar,ROWENDSIGN))//</row>
{
writeDataToTable(fpTable,"\n");
}
else if(findString(aRowChar,TABLEENDSIGN))//"</table_data>"
{
fclose(fpTable);
break;
}
}
}
if(feof(fp) != 0) break;
}
fclose(fp);
system("pause");
return 0;
}