当前位置:网站首页>bin文档读写
bin文档读写
2022-08-09 14:53:00 【Nicolas0311】
今天遇到问题。之前测试模组的时候,保存的测试数据bin文档多了2个byte数据。
解决紧急问题当然使用最熟悉方式。
于是用我准备的TestWinControl工程写了如下代码。
bin Files文件夹中有100个1~100编号的.bin文档。重新读写后
bin文件夹中生成100个已经去掉最后两个byte的.bin文档。
#include "stdafx.h"
#include <afx.h>
#include <iostream>
using namespace std;
CString GetExeDir()
{
CString ExePath, ExeDir;
TCHAR tempPath[MAX_PATH] = {
0};
GetModuleFileName(NULL,tempPath,MAX_PATH);
ExePath = tempPath;
//NOTICE: 得到的路径最后带 *\*
ExeDir = ExePath.Left( ExePath.ReverseFind(_T('\\'))+1) ;
return ExeDir;
}
void WriteBin(CString szPath, unsigned char* data, int size)
{
FILE* pFile;
if (_wfopen_s(&pFile, szPath.GetBuffer() , _T("wb+")) == 0)
{
fwrite(data, 1, size, pFile);
fclose(pFile);
szPath.Format(_T("%s write"),szPath);
cout << "write size: " << size << endl;
}
}
void ReadBin(CString szPath, unsigned char* data, int size)
{
FILE* pFile;
if (_wfopen_s(&pFile, szPath.GetBuffer() , _T("rb+")) == 0)
{
fread(data, 1, size, pFile);
fclose(pFile);
szPath.Format(_T("%s read"),szPath);
cout << "read size: " << size << endl;
}
}
int main()
{
const int size = 1406;
unsigned char data[size] = {
0};
memset(data, 0, size);
const int wsize = 1404;
unsigned char wdata[wsize] = {
0};
memset(wdata, 0, wsize);
CString exeDir = GetExeDir();
CString readPath;
CString writePath;
CreateDirectory(exeDir + _T("bin\\"), NULL);
CFileFind finder;
BOOL bw = finder.FindFile(exeDir + _T("bin Files\\*.bin"));
while(bw)
{
bw = finder.FindNextFile();
if (finder.IsDots()) continue;
if (finder.IsDirectory()) continue;
CString fileName = finder.GetFileName();
readPath = exeDir + _T("bin Files\\") + fileName;
writePath = exeDir + _T("bin\\") + fileName;
ReadBin(readPath, data, size);
memcpy(wdata, data, wsize);
WriteBin(writePath, wdata, wsize);
}
return 0;
}边栏推荐
猜你喜欢
随机推荐
在服务器上远程使用tensorboard
股票程序化交易如何理解自己的交易系统?
[Mysql]--Transaction, transaction isolation level, dirty read, non-repeatable read, phantom read analysis
WebShell简介
shell------常用小工具,sort,uniq,tr,cut
Shell编程之循环语句
数据库多表链接查询的方式
Mathematica 作图详解
DSPE-PEG-Aldehyde, DSPE-PEG-CHO, Phospholipid-PEG-Aldehyde MW: 1000
How to achieve long-term benefits through the Tongdaxin quantitative trading interface?
How to List < Map> grouping numerical merge sort
物理学专业英语(词汇整理)--------07
光线的数值追踪
Play in the cloud | The ever-changing gameplay of Tianyi cloud computer
Grad CAM model visualization
如何让你的量化交易系统具有概率优势,具有正向收益预期呢?
DBCO-PEG-DSPE, Phospholipid-Polyethylene Glycol-Dibenzocyclooctyne, Reaction Without Copper Ion Catalysis
一些需要思考的物理问题
正则化原理的简单分析(L1/L2正则化)
运算符学习









