bin文档读写
2016-04-19 14:59
211 查看
今天遇到问题。之前测试模组的时候,保存的测试数据bin文档多了2个byte数据。
解决紧急问题当然使用最熟悉方式。
于是用我准备的TestWinControl工程写了如下代码。
bin Files文件夹中有100个1~100编号的.bin文档。重新读写后
bin文件夹中生成100个已经去掉最后两个byte的.bin文档。
解决紧急问题当然使用最熟悉方式。
于是用我准备的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; }
相关文章推荐
- Shell 脚本编程陷阱
- VBS脚本写的Windows硬件检测工具分享
- 用vbscript实现隐藏任务栏图标的脚本
- 用autoit编写第一个脚本(Hello World)
- VBS调用WMI快速关闭IE的脚本
- Oracle数据库执行脚本常用命令小结
- 收集的ROS防火墙脚本
- JSP脚本漏洞面面观
- 不错的批处理脚本 第一部分
- VBS脚本加密/解密VBS脚本(简易免杀版1.1)
- 不错的批处理脚本实例代码 第二部分
- 使用脚本和批处理清除电脑中的痕迹的代码第1/2页
- 让你的脚本说话
- Lua脚本获取喜马拉雅MP3音频地址
- SQL Server中数据行批量插入脚本的存储实现
- 编程界主流脚本编程语言的比较和选择
- 批量文件查找替换功能的vbs脚本
- Powershell脚本的4种执行权限介绍
- Windows Powershell 执行文件和脚本