std::string format(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
std::vector<char> v(1024);
while (true)
{
va_list args2;
va_copy(args2, args);
int res = vsnprintf(v.data(), v.size(), fmt, args2);
if ((res >= 0) && (res < static_cast<int>(v.size())))
{
va_end(args);
va_end(args2);
return std::string(v.data());
}
size_t size;
if (res < 0)
size = v.size() * 2;
else
size = static_cast<size_t>(res) + 1;
v.clear();
v.resize(size);
va_end(args2);
}
}
void ToClipboard(std::string s)
{
OpenClipboard(0);
EmptyClipboard();
HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, s.size() + 1);
if (!hg) {
CloseClipboard();
return;
}
memcpy(GlobalLock(hg), s.c_str(), s.size() + 1);
GlobalUnlock(hg);
SetClipboardData(CF_TEXT, hg);
CloseClipboard();
GlobalFree(hg);
}
int main()
{
std::string szFileName;
std::cout << "Write file name: " << std::endl;
std::cin >> szFileName;
std::ifstream fStream(szFileName, std::ios::binary | std::ios::ate);
std::streamsize iStreamSize = fStream.tellg();
fStream.seekg(0, std::ios::beg);
std::string szBuffer;
std::vector<CHAR> vBuffer(iStreamSize);
if (fStream.read(vBuffer.data(), iStreamSize))
{
szBuffer = format("CHAR Bytes[%i]={\n\t", iStreamSize);
for (size_t i = 0; i < vBuffer.size(); i++)
{
szBuffer += format("0x%02X", (vBuffer.at(i)));
if (i != vBuffer.size() - 1) szBuffer += ",";
}
szBuffer += format("\n};");
}
ToClipboard(szBuffer);
return 0;
}