MFC的方式:
CFile file;
BOOL bData;
if (data == nullptr || data_len <= 0)
bData = FALSE;
else
bData = TRUE;
CString str1, str2, str, str4;
CString str3;
CTime t = CTime::GetCurrentTime();
str.Format(_T("%d_%d_%d.txt"), t.GetHour(), t.GetMinute(), t.GetSecond());
str1.Format(_T("d:\\test\\%s"), str);
file.Open(str1, CFile::modeCreate | CFile::modeReadWrite | CFile::modeNoTruncate);
file.SeekToEnd();
if (!bData)
{
data_len = 0;
str3 = _T("");
}
else
{
str3 = (Public::mbcs_to_unicode((char *)data)).c_str();
}
str1.Format(_T("%d"), conn_id);
str2.Format(_T("%d"), data_len);
str4 = str1 + _T(" ") + str3 + _T(" ") + str2;
file.Write(str4.GetBuffer(), str4.GetLength() * 2);
str4.ReleaseBuffer();
file.Close();
用C++ 流的方式
SYSTEMTIME now;
GetLocalTime(&now);
char timeStr[64];
sprintf_s(timeStr, sizeof(timeStr), "_%d-%02d-%02d_%02d-%02d-%02d_%03d.txt",
now.wYear, now.wMonth, now.wDay, now.wHour, now.wMinute, now.wSecond, now.wMilliseconds);
std::string filePath = "d:\\test\\";
SYSTEMTIME _now;
::GetLocalTime(&_now);
char _date[128] = { 0 };
sprintf_s(_date, 128, "%04d-%02d-%02d", _now.wYear, _now.wMonth, _now.wDay);
filePath += _date;
Public::CreateRelativeDirectorys(filePath.c_str());
filePath += timeStr;
ofstream myfile(filePath, ios::out);
unsigned char p[100000] = { 0 };
std::string strData;
strData = "";
if (data == nullptr || data_len <= 0)
{
data_len = 0;
}
else
{
for (int i = 0; i < data_len + 1; i++)
p[i] = data[i];
std::string strFile((char*)p);
strData = strFile;
}
myfile << conn_id << " \n" << strData << "\n " << data_len;
myfile.close();