#include <fstream>
#include <string>
#include <iostream>
#include <cstdio>
#include <io.h>
using namespace std;
#define DATA_SIZE 0x8000
int readini(string filename, unsigned char arrdata[DATA_SIZE])
{
int i = 0;
ifstream ifs;
ifs.open(filename, ios::in);
string str;
while(getline(ifs, str))
{
//cout << str << endl;
//int n;
//if ((n = str.rfind('x')) != string::npos)
{
string s = str.substr(7);//n-1
//cout << s << endl;
int val;
sscanf(s.data(), "0x%02X", &val);//_stscanf, s.c_str()
//cout << val << endl;
arrdata[i++] = val;
}
}
ifs.close();
return i;
}
bool writebin(string filename, unsigned char arrdata[DATA_SIZE], int datasize)
{
FILE *pf = NULL;
pf = fopen(filename.data(), "wb");
if (pf == NULL)
{
cout << " Open file fail!" << endl;
return false;
}
fwrite(arrdata, sizeof(char), datasize, pf);
fclose(pf);
pf = NULL;
return true;
}
bool ini2bin()
{
cout << "文件转换开始,请耐心等待..." << endl;
string path = "./*.ini";
_finddata_t filedata;
int handle = _findfirst(path.c_str(), &filedata);
if (-1 == handle)
{
cout << "Not find ini file!" << endl;
return 0;
}
string filename;
unsigned char arrdata[DATA_SIZE] = {0};
int datasize = 0;
do
{
filename = filedata.name;
//if (file_info.attrib == _A_SUBDIR)//文件夹
datasize = readini(filename, arrdata);
filename.replace(filename.rfind("ini"), 3, "bin");
writebin(filename, arrdata, datasize);
} while (!_findnext(handle, &filedata));
_findclose(handle);
cout << "文件转换结束!" << endl;
}
int main()
{
ini2bin();
system("pause");
return 0;
}