TXT文件学习笔记

一、头文件

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值