wxArrayString isn't getting filled Topic is solved

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.
Post Reply
CiriUp
In need of some credit
In need of some credit
Posts: 6
Joined: Sat Jun 17, 2017 9:51 am

wxArrayString isn't getting filled

Post by CiriUp »

Hello everyone,

Here I report the code:

Code: Select all

wxDir directory(directoryName);
	
	if (directory.IsOpened()) {
	
		wxArrayString* exts = new wxArrayString();
		exts->Add(".bmp");
		exts->Add(".dib");
		exts->Add(".jpeg");
		exts->Add(".jpg");
		exts->Add(".jpe");
		exts->Add(".jp2");
		exts->Add(".png");
		exts->Add(".webp");
		exts->Add(".pbm");
		exts->Add(".pgm");
		exts->Add(".ppm");
		exts->Add(".sr");
		exts->Add(".ras");
		exts->Add(".tiff");
		exts->Add(".tif");
		
		this->files = new wxArrayString();
		
		for (int i = 0; i < exts->GetCount(); i++ ) {
			wxString filename;
			bool res = directory.GetFirst(&filename);
			
			if (res) {
				do {
					if (filename.EndsWith(exts->Item(i))) {
					wxMessageBox(filename, "", wxOK);
						files->Insert(filename, size);
						size++;
					}
					res=directory.GetNext(&filename);
				} while (res);
			}
		}
		wxString str = wxString::Format("The first element is: %s The size of the array is: %d %d", files->Item(0), files->size(), size);
		wxMessageBox(str, "", wxOK);
		exts->Empty();
	}
My problem is that the wxArrayString files isn't getting filled when I call the function Insert(filename, size).
The first message box is working, so I know that portion of code is executed, but when the second message box is going to be created, the program throws an assertion failure due to index out of bound. I've tried using Add() instead of Insert, adding the "this->" when creating and using files, but nothing worked. The aim of this piece of code is enumerating the files into a given directory.

Can anyone help me or suggest me some other way to implement the code?

Thank You in advice.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxArrayString isn't getting filled

Post by doublemax »

I don't see "size" getting initialized to 0.

Apart from that, wxDir::GetAllFiles() would make your life easier:
http://docs.wxwidgets.org/trunk/classwx ... 28d0ada848
Use the source, Luke!
CiriUp
In need of some credit
In need of some credit
Posts: 6
Joined: Sat Jun 17, 2017 9:51 am

Re: wxArrayString isn't getting filled

Post by CiriUp »

Hi, I forgot to mention that this is a constructor of a c++ class and that "size" is initialized in the header file:

Code: Select all

int size = 0;
doublemax wrote: I don't see "size" getting initialized to 0.

Apart from that, wxDir::GetAllFiles() would make your life easier:
http://docs.wxwidgets.org/trunk/classwx ... 28d0ada848
I've tried it: same result.

I'm using wxWidgets 3.0 on a Linux machine.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxArrayString isn't getting filled

Post by doublemax »

Please post complete code in context, you must be making a simple mistake somewhere.

This code works for me (you need to change the hardcoded path of the directory):

Code: Select all

#include "wx/dir.h"
///

wxArrayString exts;
exts.Add("*.bmp");
exts.Add("*.dib");
exts.Add("*.jpeg");
exts.Add("*.jpg");
exts.Add("*.jpe");
exts.Add("*.jp2");
exts.Add("*.png");
exts.Add("*.webp");
exts.Add("*.pbm");
exts.Add("*.pgm");
exts.Add("*.ppm");
exts.Add("*.sr");
exts.Add("*.ras");
exts.Add("*.tiff");
exts.Add("*.tif");

wxArrayString files;
wxString path = "d:\\_pics\\";
for(int i=0; i<exts.Count(); i++)
{
  wxDir::GetAllFiles( path, &files, exts[i] );
}
wxLogMessage( "found %d files", files.GetCount() );

for(int i=0; i<files.Count(); i++)
{
  wxLogMessage("%s", files[i]);
}
Use the source, Luke!
CiriUp
In need of some credit
In need of some credit
Posts: 6
Joined: Sat Jun 17, 2017 9:51 am

Re: wxArrayString isn't getting filled

Post by CiriUp »

Hi,
I've managed to solve the problem. It was only a matter of pointers and how to use them.

Sorry for wasting your time

Thank You very much.
Post Reply