loading and saving wxArrayString

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
fgleich
Earned a small fee
Earned a small fee
Posts: 11
Joined: Fri Jan 07, 2005 4:57 pm
Location: NM USA

loading and saving wxArrayString

Post by fgleich »

Does anyone have an easy way to load and save a wxArrayString from and to a file ?
Thanks :D
phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany

Post 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.
fgleich
Earned a small fee
Earned a small fee
Posts: 11
Joined: Fri Jan 07, 2005 4:57 pm
Location: NM USA

Post by fgleich »

Thanks for the quick reply. :D
emarti
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 210
Joined: Sat May 07, 2005 8:24 pm
Location: Eskisehir, TURKEY

Post 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; 

} 
- T U R K E Y ?
- I love this country!

WebSites:
http://mebt.sourceforge.net/
http://wxquran.sourceforge.net/
fgleich
Earned a small fee
Earned a small fee
Posts: 11
Joined: Fri Jan 07, 2005 4:57 pm
Location: NM USA

Post by fgleich »

Thanks also for assistance, I did something similar.
Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands

Post by Jorg »

Try wxArchive, it allows you to serialize wxArrayStrings to any wxOutputStream.

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

- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb