C++参数保存到txt(使用文件流ofstream )
//2024.1.24 将对应的文件参数保存到txt
bool CMachineSetting::OnSaveParamToTetx(ui::EventArgs*msg)
{
if (g_pMainFrame->m_strFileName == L"")
{
//文件名为空 弹出提示框
nim_comp::ShowMsgBox(GetHWND(), nim_comp::MsgboxCallback(), _T("STRID_MSG_FILENAMEISFULL"));
return false;
}
TSTRING strTitle = m_pMultilingual->GetStringViaID(L"STRID_CCDCALI_BUTTON_SETPATH");
TCHAR szBuffer[MAX_PATH] = { 0 };
BROWSEINFO bi = { 0 };
bi.hwndOwner = GetHWND();//拥有着窗口句柄,为NULL表示对话框是非模态的,实际应用中一般都要有这个句柄
bi.pszDisplayName = szBuffer;//接收文件夹的缓冲区
//bi.lpszTitle = strTitle.c_str();//标题
bi.ulFlags = BIF_NEWDIALOGSTYLE | BIF_USENEWUI;
bi.lpfn = (_BrowseCallbackProc);
//bi.lParam = (LPARAM)(LPCTSTR)(strFolder.c_str());
LPITEMIDLIST idl = SHBrowseForFolder(&bi);
if (!SHGetPathFromIDList(idl, szBuffer))
return false;
// 定义参数和值
string param1 = "Y阵列数量";
string param2 = "Y阵列间距";
string param3 = "色标跳距";
string param4 = "寻标间隔";
string param5 = "X偏移";
string param6 = "Y偏移";
string param7 = "Y向分距";
//文件名
int index = g_pMainFrame->m_strFileName.find_last_of('.');
wstring _filename = g_pMainFrame->m_strFileName.substr(0, index);
CString _str;
_str = szBuffer;
_str += "\\";
_str += _filename.c_str();
_str += ".txt";
// 创建文件流对象并打开文件
ofstream outfile(_str);
if (!outfile.is_open()) {
//文件打开失败
return 0;
}
wstring _wstr1, _wstr2, _wstr3, _wstr4, _wstr5, _wstr6, _wstr7;
g_pMainFrame->OnGetParam(_wstr1, _wstr2, _wstr3, _wstr4);
_wstr5 = m_edit_xaxis->GetText();
_wstr6 = m_edit_yaxis->GetText();
// 创建字符串流对象并写入参数和值
_wstr7 = g_pMainFrame->GetYDistance();
stringstream ss;
ss << param1 << "=" << stoi(_wstr1) << endl;
ss << param2 << "=" << stof(_wstr2) << endl;
ss << param3 << "=" << stof(_wstr3) << endl;
ss << param4 << "=" << stoi(_wstr4)<< endl;
ss << param5 << "=" << stof(_wstr5) << endl;
ss << param6 << "=" << stof(_wstr6) << endl;
ss << param7 << "=" << stof(_wstr7) << endl;
// 将字符串流对象的内容写入文件
outfile << ss.str();
// 关闭文件流对象
outfile.close();
}