Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 12:06

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Problem mit Listen

Discussion on Problem mit Listen within the C/C++ forum part of the Coders Den category.

Closed Thread
 
Old   #1
 
NikM's Avatar
 
elite*gold: 0
Join Date: Aug 2010
Posts: 972
Received Thanks: 1,583
Question Problem mit Listen

Hi Leute.
Ich habe ein Problem mit dem Abspeichern und dem Aufrufen von einer Liste aus einer externen Datei.
Wenn ich erst abspeicher und direkt wieder lade ohne das Programm vorher zu schließen funktioniert alles reibungslos.
Wenn ich aber die Liste erst abspeicher, das Programm schließe, neu starte und dann versuche die Liste wieder zu laden stürzt das Programm entweder ab oder ich bekomme nur Teile des Abgespeicherten wieder.
Ich hoffe jemand von euch kann mir helfen

Meine Speicherfunktion
Code:
void cVisualBackpack::SaveList (list <sItem> listX, char *szFileName)
{
	ofstream Output (szFileName, ios::binary);
	int iSize = listX.size ();
	list <sItem>::iterator listIt;

	Output.write ((char *)(&iSize), sizeof (iSize));

	for (listIt = listX.begin () ; listIt != listX.end () ; ++listIt)
		Output.write ((char *)(& (*listIt)), sizeof (sItem));

	Output.close ();
}
Meine Load Funktion
Code:
list <sItem> cVisualBackpack::LoadList (char *szFileName)
{
	
	ifstream Input (szFileName, ios::binary);
	int iSize = 0;
	list <sItem> lTempList;
	lTempList.clear ();
	sItem TempItem;

	Input.read ((char *)(&iSize), sizeof (iSize));

	for (int i = 0 ; i < iSize ; ++i)
	{
		Input.read ((char *)(&TempItem), sizeof (sItem));
		lTempList.push_back (TempItem);
	}

	Input.close ();

	return (lTempList);
}
Der Aufruf
Code:
typedef struct
{
	const char *szItem;
	float fGewicht;
} sItem;

sItem ItemsAndreas [] =
{
	{"Hose", 0.53f},
	{"Socken", 0.34f},
	{"Jacke", 0.76f},
	{"Rucksack", 2.22f}
};

class cVisualBackpack
{
	public:
		void AddItem (HWND hListHwnd, sItem Item);
		void RemoveItem (HWND hListHwnd);
		void RemoveAll (HWND hListHwnd);
		void SaveList (list <sItem> listX, char *szFileName);
		list <sItem> LoadList (char *szFileName);
		int GetSelItem (HWND hListHwnd);
		float getGewicht ();
		list <sItem> m_lsItemAndreas;
		list <sItem> m_lsItemUte;
		list <sItem>::iterator i;

	private:
		float getFloat (char *szBuffer);
		float m_fGesamtGewicht;
		list <float> m_lfGewichte;
};


for (int x = 0 ; x < ARRAYSIZE (ItemsAndreas) ; ++x)
	myBP.m_lsItemAndreas.push_back (ItemsAndreas [x]);

myBP.SaveList (myBP.m_lsItemAndreas, "Andreas.sav");

myBP.m_lsItemAndreas = myBP.LoadList ("Andreas.sav");

if (!myBP.m_lsItemAndreas.empty ())
	for (myBP.i = myBP.m_lsItemAndreas.begin () ; myBP.i != myBP.m_lsItemAndreas.end () ; ++myBP.i)
		myBP.AddItem (hListBoxSchrank, *myBP.i);
MfG NikM
NikM is offline  
Old 02/19/2012, 12:53   #2
 
elite*gold: 115
Join Date: Oct 2007
Posts: 9,390
Received Thanks: 12,344
Dein Struct, welches du abspeicherst, besteht auch aus einem Pointer. Beim Speichern speicherst du allerdings nur den Pointer ab und nicht den String, auf den er eigentlich zeigt. Deswegen ist es kein Problem, wenn du während das Programm läufst lädst und speicherst; die Pointer zeigen immer noch zu gültigen Strings. Wenn du aber dein Programm neu startest, zeigen die Pointer auf zufällige Daten im Speicher.

Du solltest den String speichern und nicht den Pointer davon.
ms​ is offline  
Thanks
1 User
Old 02/19/2012, 12:57   #3
 
NikM's Avatar
 
elite*gold: 0
Join Date: Aug 2010
Posts: 972
Received Thanks: 1,583
Danke du hast meinen Tag gerettet
#closerequest
NikM is offline  
Old 02/19/2012, 17:18   #4
 
elite*gold: 5
Join Date: Sep 2006
Posts: 385
Received Thanks: 218
Als kleiner Tipp:
Du solltest generell nicht mit Pointern arbeiten. Anstelle von char arrays (char *) ist std::string zu benutzen und solltest du wirklich nicht um einen Pointer herum kommen, dann ist std::make_shared oder std::make_unique zu benutzen.

Wer mit Pointern arbeitet macht in der Regel etwas falsch.
Nightblizard is offline  
Closed Thread


Similar Threads Similar Threads
listen to this!
02/17/2012 - Need for Speed World - 2 Replies
Hey guy,s There is SB vendors in world! Don,t believe them they all are fake! I have stolen ACC with lvl 40!!! :@ i,m realy angry to my self i trusted them :@ Don,t give them aneything!
Listen UP.....
05/30/2011 - 9Dragons - 6 Replies
Ok Here are a few Safe and Legit Selling Sites and i will give a description on each. Welcome to Gaming Goods.! - Gaming Goods. Safe! ice-star Safe! shop9d.com Safe but not available okaygoods Safe but a lot of info required to get items.
Listen Up
09/12/2009 - 9Dragons - 20 Replies
Anyone little noob who comes around asking the same question that we have seen over and over again... DO NOT HELP THEM!!!!, why you ask? cause those noobs MOST likely didn't even search this whole forum themself for the answers.. Question such as: "Where is download link for game" "Where is patch" "Where is NDTEXTDB_US.XSD for translate" "Where can I find a bot or trainer"
WoW Listen
12/28/2007 - WoW Private Server - 1 Replies
hiho ich hatte ma ne liste wo items objekte und spells etc alles aufgelistet wurden, hat jemand zufällig so ne liste bzw. listen?
PLEASE EVERYONE LISTEN
05/07/2007 - Silkroad Online - 11 Replies
I understand everyone wants to level quick and even my self i have tried a silkroad bot before, but botting has gotten way out of hand now, please do as gzp did and stop releasing the isrobot cracked. Its not a matter of morality it is just now EVERY server is becoming crowded even at night, i cant play at 1:30-11:59pm, it has just gotten too far out of hand please for the love of god stop releasing this bot. It is in way too many players hands and it has ruined silkroad online. Maybe i am just...



All times are GMT +2. The time now is 12:06.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.