#include <map>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;
int main(int argc,char **argv)
{
//打开 表名.txt
fstream TableName;
TableName.open("C:\\Documents and Settings\\Administrator\\Desktop\\20120820\\Table.txt",ios::in);
if(!TableName.is_open())
{
cout << "failure to open"<<endl;
}
//打开 列名.txt
fstream ColumnName;
ColumnName.open("C:\\Documents and Settings\\Administrator\\Desktop\\20120820\\Column.txt",ios::in);
if(!ColumnName.is_open())
{
cout << "failure to open"<<endl;
}
vector<string> columnV;
string ColumnLine;
while(getline(ColumnName,ColumnLine))
{
columnV.push_back(ColumnLine);
}
cout <<"列数:" <<columnV.size()<<endl;
//打开 类型.txt
fstream TypeName;
TypeName.open("C:\\Documents and Settings\\Administrator\\Desktop\\20120820\\Type.txt",ios::in);
if(!TypeName.is_open())
{
cout << "failure to open!" << endl;
}
vector<string> TypeV;
string TypeLine;
while(getline(TypeName,TypeLine))
{
TypeV.push_back(TypeLine);
}
cout <<"类型:" <<TypeV.size()<<endl;
//定义 结果集
fstream CreateTable;
CreateTable.open("C:\\Documents and Settings\\Administrator\\Desktop\\20120820\\CreateTable.txt",ios::out);
if(!CreateTable.is_open())
{
cout << "failure to open"<<endl;
}
//统计表名,及每个表名的个数,也就是表的列数
map<string,int> TableNameNumber;
string line;
while(getline(TableName,line))
{
pair<map<string,int>::iterator,bool> ret = TableNameNumber.insert(make_pair(line,1));
if(!ret.second)
{
++ret.first->second;
}
}
//构建create table SQL语句
string Column;
string Type;
int i;
vector<string>::iterator columnIter = columnV.begin();
vector<string>::iterator typeIter = TypeV.begin();
cout.setf(ios::left); //设置对齐方式为left
cout.width(8); //设置宽度为7,不足用空格填充
for(map<string,int>::iterator begin = TableNameNumber.begin();
begin != TableNameNumber.end(); begin++)
{
cout << "create table "+(*begin).first << endl;
cout << "(" << endl;
for(
i=0;
i<(*begin).second;
i++,columnIter++,typeIter++
)
{
cout <<"\t"<<setw(10)<<left <<(*columnIter);
cout.unsetf(ios::left);
cout<<"\t" << (*typeIter)<< "," <<endl;
}
cout << ")" << endl;
}
TableName.close();
ColumnName.close();
TypeName.close();
}