L o a d i n g . . .
主打一个C++
文章详情

C+很多年前的将文件转十六进制资源.h文件的代码~~~

Posted on 2023-02-02 16:05:41 by 主打一个C++

不要笑  ,还是能正常跑起来的😀~~~~~

C++代码展示:

#include <iostream>
#include <windows.h>
#include <istream>
#include <string>
#pragma warning (disable:4996)
int main(int arg, char* argv[])
{
	FILE* pFile = nullptr;
	char* buffer = nullptr;
	char* data = nullptr;
	std::string dataStr;
	if (arg == 2) {//命令行启动传入文件路径
		data = argv[1];
		if (data == nullptr) {
			printf("argv[1] is null!\n");
			system("pause");
			return -1;
		}
	}
	else {//命令行启动没有传入文件路径
		do
		{
			printf("Please enter the file path: ");
			std::getline(std::cin, dataStr);
			if (dataStr.empty()) {
				printf("input is empty!\n\n");
			}
		} while (dataStr.empty());
		data = const_cast<char*>(dataStr.c_str());
		if (data == nullptr){
			printf("data is error!\n");
			system("pause");
			return -1;
		}
	}
	do
	{
			char fileNmae[_MAX_PATH] = { 0 };
			char* pNmae = fileNmae;
			char varName[64] = { 0 };
			memcpy(fileNmae, data, strlen(data));
			bool dian = true;
			//get fileName
			for (size_t i = strlen(pNmae); i > 0; i--)
			{
				if (dian && (fileNmae[i] == '.')) {
					fileNmae[i] = '\0';
					dian = false;
					continue;
				}
				if (fileNmae[i] == '\\') {
					pNmae = &fileNmae[i + 1];
					snprintf(varName, sizeof(varName), "HEX_%s", pNmae);
					memcpy(pNmae + strlen(pNmae), ".h", 3);
					break;
				}
			}
			if (*fileNmae == 0) {
				printf("get save fileName[%s] failed!\n", fileNmae);
				break;
			}

			pFile = fopen(data, "rb");
			if (pFile == nullptr) {
				printf("%s open failed!\n", data);
				break;
			}
			_fseeki64(pFile, 0, SEEK_END);
			size_t len = _ftelli64(pFile);
			_fseeki64(pFile, 0, SEEK_SET);
			if (len < 1) {
				printf("file length error.\n");
				break;
			}
			buffer = new char[len];
			size_t rlen = fread(buffer, 1, len, pFile);
			if (rlen != len) {
				printf("fread length error[len=%lld][rlen=%lld]\n", len, rlen);
				break;
			}
			fclose(pFile);
			pFile = nullptr;

			DeleteFileA(fileNmae);
			//直接输出文件
			pFile = fopen(fileNmae, "a+");
			if (pFile == nullptr) {
				printf("save failed!\n");
				break;
			}
			size_t t = 17;
			fprintf(pFile, "const unsigned char %s[%lld]{\n", varName, rlen);
			for (size_t i = 0; i < rlen; i++)
			{
				//printf("%d,", buffer[i]);
				fprintf(pFile, "0x%02X,", buffer[i] & 0xFF);
				if (!(t++ % 16)) {
					fprintf(pFile, "\n");
				}
			}
			fprintf(pFile, "};");
	} while (false);

	if (pFile != nullptr)
		fclose(pFile);
	if (buffer != nullptr)
		delete[] buffer;

}


*转载请注明出处:原文链接:https://cpp.vin/page/99.html

作者近期文章
  • 随手笔记
  • 主打一个C++   2025-01-11 20:02:01
  • 都2000000025年了。还有不能随意访问guthub的,仔细看。在国内其实是可以正常访问的,gfw并没屏蔽。这里给出其中一个简单直接的方法稳定访问。1. 随便百度一个”dn
提示
×
确定
数据库执行: 8次 总耗时: 0.01s
页面加载耗时: 



wechat +447752296473
wechat cpp-blog