[問題] C++ 儲存XML多筆資料的問題

作者: jayzhuang (Jay)   2019-08-20 14:37:33
開發平台(Platform): (Ex: Win10, Linux, ...)
Win10
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
vs2019
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
問題(Question):
上次感謝大家提醒我透過secedit轉檔的格式,改成ini;
並使用ini parser,讓我找到:GetPrivateProfileString/WritePrivateProfileString
目前我的Pwdword.ini,可透過這兩個去做讀取後,再存入到XML格式的檔案中。
但是我一個奇怪狀況,之前明明可以直接在顯示多筆資料的,
現在顯示卻永遠都顯示最後一筆資料.....
餵入的資料(Input):
我將Pwdword.ini內,三個項目等號的資料透過GetPrivateProfileString,
取得之後,一個一個測試,確認有資料入:
(下圖為Pwdword.ini的內容,紅色框框圈起三個資料是我抓取的)
https://imgur.com/PALPOII
下列程式碼是三筆資料的格式轉檔後,透過程式碼給儲存xml的Function
Sleep(10000);
//把要回傳的LGPO_xml細項內容儲存到這
string LGPO_XML_FilePath = GetFilePath() + "LGPO_Item.xml";
//寫入LGPO項目的內容
Save_LGPO_Item LGPO_Item_Content;
LGPO_Item_Content.LGPO_class = 1;
char Pw01[] = "MinimumPasswordAge";
char Pw02[] = "MaximumPasswordAge";
char Pw03[] = "MinimumPasswordLength";
char Pw04[] = "PasswordComplexity";
char Pw05[] = "PasswordHistorySize";
char Pw06[] = "ClearTextPassword";
char Pw07[] = "LockoutBadCount";
char Pw08[] = "LockoutDuration";
char Pw09[] = "ResetLockoutCount";
char Pw10[] = "ForceLogoffWhenHourExpire";
char Pw11[] = "NewAdministratorName";
char Pw12[] = "NewGuestName";
//宣告要資料類型名稱與,透過GetPrivateProfileString去抓取名稱後面的資料
CString MinimumPasswordAge_value;
::GetPrivateProfileString(_T("System Access"), _T("MinimumPasswordAge"), 0, MinimumPasswordAge_value.GetBuffer(MAX_PATH), MAX_PATH, _T("C:\\LGPO\\Pwdword.ini"));
MinimumPasswordAge_value.ReleaseBuffer();
//之後要把CString 轉成string的資料類型,方可回傳。
std::string MinimumPasswordAge_string(CW2A(MinimumPasswordAge_value.GetString()));
CString MaximumPasswordAge_value;
::GetPrivateProfileString(_T("System Access"), _T("MaximumPasswordAge"), 0, MaximumPasswordAge_value.GetBuffer(MAX_PATH), MAX_PATH, _T("C:\\LGPO\\Pwdword.ini"));
MaximumPasswordAge_value.ReleaseBuffer();
std::string MaximumPasswordAge_string(CW2A(MaximumPasswordAge_value.GetString()));
CString NewAdministratorName_value;
::GetPrivateProfileString(_T("System Access"), _T("NewAdministratorName"), 0 , NewAdministratorName_value.GetBuffer(MAX_PATH),MAX_PATH, _T("C:\\LGPO\\Pwdword.ini"));
NewAdministratorName_value.ReleaseBuffer();
std::string NewAdministratorName_string(CW2A(NewAdministratorName_value.GetString()));
LGPO_Item_Content.Item_name = Pw01;
LGPO_Item_Content.Item_value = MinimumPasswordAge_string;
LGPO_Item_Content.Item_name = Pw02;
LGPO_Item_Content.Item_value = MaximumPasswordAge_string;
LGPO_Item_Content.Item_name = Pw11;
LGPO_Item_Content.Item_value = NewAdministratorName_string;
Save_LGPO_XML(&LGPO_Item_Content, LGPO_XML_FilePath);
預期的正確結果(Expected Output):
(下圖是三筆資料的顯示)
https://imgur.com/c6CvK5k
錯誤結果(Wrong Output):
(結果永遠都只有一筆)
https://imgur.com/OvhZGsW
程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔)
這是我把資料存入XML的Funcion,我把回傳值得Function與Save_XML的Function分開兩種
作者: firejox (Tangent)   2019-08-20 17:14:00
不宣告3個嗎?你TiXmlElement("Item")只有1個,傳入的也只有1個當然顯示一個
作者: joechen1008 (:))   2019-08-20 23:45:00
伸手牌?
作者: firejox (Tangent)   2019-08-21 00:01:00
你要傳多個,用陣列或stl container 都行

Links booklink

Contact Us: admin [ a t ] ucptt.com