常用代碼片段[待補充]
讀取檔案進行解析處理
//解析調試的資料(測試)
void testParaData()
{
//讀取檔案
FILE *stream;
if ((stream = fopen("./FrameInfo.txt", "rb")) == NULL)
{
WirteLogLocal("Cannot open output file.\n");
}
MicsIPCStreamKey key;
key.iStreamType = 0;
char mzCode[] = "33059151001320300016";
memcpy(key.szCamCode, mzCode, sizeof(mzCode));
int dataLen = 0;
int ret = 0;
//解析資料
while (true)
{
ret = fread(&dataLen, 1, 4, stream);
if (ret != 4)
{
break;
}
char* pVideoData = new char[dataLen];
ret = fread(pVideoData, 1, dataLen, stream);
if (ret < dataLen)
{
break;
}
GetStreamCallBack(122, 0, pVideoData, dataLen, (void*)(&key));
delete pVideoData;
}
fclose(stream);
}
//點分十進制轉數字
inet_addr("192.168.1.10")
//數字轉點分十進制
sockaddr_in devAddr = {};
memcpy(&(devAddr.sin_addr.S_un), &dwIP, sizeof(DWORD));
std::string strIP = std::string("IP=") + inet_ntoa(devAddr.sin_addr) + std::string("\n");
#include <Psapi.h>
#pragma comment ( lib, "Psapi.lib" )
#define APP_MUTEX _T("XXXXX")
HANDLE hAppMutex = CreateMutex(NULL, TRUE, APP_MUTEX);
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
CloseHandle(hAppMutex);
hAppMutex = NULL;
//MessageBox(_T("The program has been running!"));
exit(0);
}
#include <string>
#include <fstream>
void WriteLog(std::string logata)
{
std::ofstream fout;
fout.open("./log.txt", std::ios::binary | std::ios::app);
if (!fout)
{
return;
}
fout << logata;
fout.close();
}
void WriteFile(std::string data)
{
FILE *stream = NULL;
if ((stream = fopen("./log.txt", "wb+")) == NULL)
{
return;
}
fwrite(data.c_str(), data.size(), 1, stream);
fclose(stream);
}
- 字元分割
- 參考:https://www.jianshu.com/p/5876a9f49413
std::vector<std::string> strSplit(const std::string &str, const std::string &pattern)
{
std::vector<std::string> res;
if (str == "")
return res;
//在字元串末尾也加入分隔符,友善截取最後一段
std::string strs = str + pattern;
size_t pos = strs.find(pattern);
while (pos != strs.npos)
{
std::string temp = strs.substr(0, pos);
res.push_back(temp);
//去掉已分割的字元串,在剩下的字元串中進行分割
strs = strs.substr(pos + 1, strs.size());
pos = strs.find(pattern);
}
return res;
}
void getResourcePath(HINSTANCE hInstance, char pPath[1024])
{
char szPathName[1024];
char szDriver[64];
char szPath[1024];
GetModuleFileNameA(hInstance, szPathName, sizeof(szPathName));
_splitpath(szPathName, szDriver, szPath, 0, 0);
sprintf(pPath, "%s%s", szDriver, szPath);
}
//使用:
char szPath[1024];
getResourcePath(0,szPath);