一、头文件
#include "shlwapi.h"
#pragma comment(lib,"shlwapi.lib") //添加相应的lib库,PathFileExists()函数需要,这个PathFileExists()函数用于测试文件夹是否已存在
#include <fstream> // c++ 文件IO头
#include <iostream>
#include <iomanip>
using namespace std;
二、文件夹建立
dlg1.DoModal();
//****************************创建文件夹**********************************//
TCHAR *p =new TCHAR[1000];
GetCurrentDirectory(1000,p);
//strPath_txt = _T("D:\\hex转txt结果");
strPath_txt = p;
delete p;
strPath_txt +="\\hex转txt结果";
if(!PathFileExists(strPath_txt))//文件夹不存在则创建
{
CreateDirectory(strPath_txt,NULL);
}
三、TXT文件建立和操作
if(m_StrUpdateFilePath.GetLength()<0x08)
{
m_StrResult.Empty();
GetLocalTime(&CurrTime);
StrCTime.Format(_T("[%4d-%02d-%02d %02d:%02d:%02d]>>"),CurrTime.wYear,CurrTime.wMonth,CurrTime.wDay,CurrTime.wHour,CurrTime.wMinute,CurrTime.wSecond);
m_StrResult += StrCTime;
m_StrResult += _T("请载入文件!\r\n");
m_Result.LineScroll(m_Result.GetLineCount());
m_Result.SetSel(m_Result.GetTextLength(),m_Result.GetTextLength());
m_Result.ReplaceSel(m_StrResult);
s.Format(_T("Number of Lines: %d"),m_Result.GetLineCount()-1);
s1.Format(_T("已就绪"));
m_bar.SetPaneText(0,s);
m_bar.SetPaneText(1,s1);
return;
}
CString strPath;
CString a;
a.Format(_T("%d"),count_txt);
strPath+=strPath_txt+"\\"+a+_T(".txt");
ofstream my_fout(strPath);
// my_fout<<hex;
if(!my_fout)
{
m_StrResult.Empty();
GetLocalTime(&CurrTime);
StrCTime.Format(_T("[%4d-%02d-%02d %02d:%02d:%02d]>>"),CurrTime.wYear,CurrTime.wMonth,CurrTime.wDay,CurrTime.wHour,CurrTime.wMinute,CurrTime.wSecond);
m_StrResult += StrCTime;
m_StrResult += _T("文件创建失败!\r\n");
m_Result.LineScroll(m_Result.GetLineCount());
m_Result.SetSel(m_Result.GetTextLength(),m_Result.GetTextLength());
m_Result.ReplaceSel(m_StrResult);
s.Format(_T("Number of Lines: %d"),m_Result.GetLineCount()-1);
s1.Format(_T("已就绪"));
m_bar.SetPaneText(0,s);
m_bar.SetPaneText(1,s1);
my_fout.close(); // 执行完操作后关闭文件句柄
return;
}
if(!my_fout.is_open())
{
m_StrResult.Empty();
GetLocalTime(&CurrTime);
StrCTime.Format(_T("[%4d-%02d-%02d %02d:%02d:%02d]>>"),CurrTime.wYear,CurrTime.wMonth,CurrTime.wDay,CurrTime.wHour,CurrTime.wMinute,CurrTime.wSecond);
m_StrResult += StrCTime;
m_StrResult += _T("文件打开失败!\r\n");
m_Result.LineScroll(m_Result.GetLineCount());
m_Result.SetSel(m_Result.GetTextLength(),m_Result.GetTextLength());
m_Result.ReplaceSel(m_StrResult);
s.Format(_T("Number of Lines: %d"),m_Result.GetLineCount()-1);
s1.Format(_T("已就绪"));
m_bar.SetPaneText(0,s);
m_bar.SetPaneText(1,s1);
my_fout.close(); // 执行完操作后关闭文件句柄
return;
}
unsigned long i=0;
unsigned char m,n;
for(i=0;i<usb_data_len;i++)
{
m=(updatedata[i]>>4) & 0x0F;
n=updatedata[i] & 0x0F;
if(m<0x0A)
{
m+=0x30;
}
else
{
m+=0x37;
}
if(n<0x0A)
{
n+=0x30;
}
else
{
n+=0x37;
}
my_fout << m;
my_fout << n;
my_fout << "\n";
}
my_fout.close(); // 执行完操作后关闭文件句柄
m_StrResult.Empty();
GetLocalTime(&CurrTime);
StrCTime.Format(_T("[%4d-%02d-%02d %02d:%02d:%02d]>>"),CurrTime.wYear,CurrTime.wMonth,CurrTime.wDay,CurrTime.wHour,CurrTime.wMinute,CurrTime.wSecond);
m_StrResult += StrCTime;
m_StrResult += strPath+_T("\r\n");;
m_Result.LineScroll(m_Result.GetLineCount());
m_Result.SetSel(m_Result.GetTextLength(),m_Result.GetTextLength());
m_Result.ReplaceSel(m_StrResult);
str_programmedata.Empty();
s.Format(_T("Number of Lines: %d"),m_Result.GetLineCount()-1);
s1.Format(_T("已就绪"));
m_bar.SetPaneText(0,s);
m_bar.SetPaneText(1,s1);
count_txt++;