Page 1 of 1

loading and saving wxArrayString

Posted: Wed Apr 12, 2006 5:43 pm
by fgleich
Does anyone have an easy way to load and save a wxArrayString from and to a file ?
Thanks :D

Posted: Wed Apr 12, 2006 5:46 pm
by phlox81
write all entries to file.
Before store how much items are there.
Then, when loading, read first the number of items,
then read the items and append to your array.

I have written a helperfunction a few month ago, to save STL like containers:

Code: Select all

template<class cont> void contwriter(std::ostream& out,const cont& container)
{
	out << container.size() <<' ';
    typename cont::iterator begin=container.begin();
    typename cont::iterator end = container.end();
  //  if(begin == end) return;
    for(;begin != end; ++begin)
        out << *begin << ' ';
}

template<class cont, class T> void contreader(std::istream& in, cont& container)
{
	std::size_t n;
	in >> n;
	if(n > 0)
	{
		T t;
		for(std::size_t i =0; i < n; ++i)
		{
			in >> t;
			container.push_back(t);
        }
    }
}
its basicly the same with wxStringArray, just a different interface.

Posted: Sat Apr 15, 2006 3:50 pm
by fgleich
Thanks for the quick reply. :D

Posted: Sun Apr 16, 2006 3:25 am
by emarti

Code: Select all

bool SaveArrayStringToFile(wxString strFileName,const wxArrayString &aryStr) 
{ 
wxTextFile txtFile(strFileName); 
txtFile.Create(); 

for(int i=0;i<aryStr.GetCount();i++) 
{ 
txtFile.AddLine(aryStr.Item(i)); 
} 
txtFile.Close(); 
return true; 
} 



bool LoadArrayStringFromFile(wxString strFileName,wxArrayString &aryStr) 
{ 
aryStr.Clear(); 
wxTextFile txtFile(strFileName); 
txtFile.Open(); 
if (txtFile.IsOpened() == FALSE) 
{ 
return false; 
} 
int TotalLines = txtFile.GetLineCount(); 
for(int i=0;i<txtFile.GetLineCount();i++) 
{ 
aryStr.Add(txtFile.GetLine(i)); 
} 
return true; 

} 

Posted: Mon Apr 17, 2006 3:55 pm
by fgleich
Thanks also for assistance, I did something similar.

Posted: Mon Apr 17, 2006 6:00 pm
by Jorg
Try wxArchive, it allows you to serialize wxArrayStrings to any wxOutputStream.

http://wastebucket.solidsteel.nl/cms/mo ... k=WiwiHome

- Jorgen