读ini,写bin,遍历文件夹下ini文件

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值