string dynamic memory

Hi,

I'm trying to make an string pointer to contain multiple filenames:
string* myList = new string[max];
But when I add the second, third, fourth, ... value using:
1
2
for (int i = 0; i < max; i++)
    myList[i] = ...somevalue...;

the myList variable will be blank.
What am I doing wrong? If this can't work, what is another way to store filenames as some kind of list?
There doesn't seem to be anything wrong in the few lines you've posted.
Try posting all of it, if it's not too long.

You could also try vector<string>. That way you wouldn't have to bother with memory allocation.
This is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <string>
#include "Exception.h"

using namespace std;

#ifdef OS_WINDOWS

#include <windows.h>

namespace PEARL
{
	unsigned int ListDirectories(string path, string* dirs, unsigned int max)
	{
		//Initializing
		string* foundDirs = new string[max];
		unsigned int numFoundDirs = 0;
		HANDLE hFile = INVALID_HANDLE_VALUE;
		WIN32_FIND_DATAA wfd;
		path += "/*";

		//Start with the first file
		hFile = FindFirstFileA(path.c_str(), &wfd);
		if (hFile == INVALID_HANDLE_VALUE)
		{
			if (errno != ERROR_FILE_NOT_FOUND)
			{
				string errorMessage = "Unable to find the first directory for listing directory \"";
				errorMessage += path;
				errorMessage += "\"";
				throw new Exception(errno, errorMessage);
			}
			else //Return empty.
			{
				dirs = new string[0];
				return 0;
			}
		}
		if ( ( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) > 0 ) //This is true when the found file, is actually a directory.
		{
			foundDirs[0] = wfd.cFileName;
			numFoundDirs++;
		}
	
		//And the rest of the files
		while (FindNextFileA(hFile, &wfd) != 0 && numFoundDirs < max)
		{
			if ( ( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) > 0 )
			{
				foundDirs[numFoundDirs] = wfd.cFileName;
				numFoundDirs++;
			}
		}
		if (errno != ERROR_NO_MORE_FILES && errno != 0)
		{
			string errorMessage = "Unable to find a directory for listing directory \"";
			errorMessage += path;
			errorMessage += "\"";
			throw new Exception(errno, errorMessage);
		}

		//finish
		dirs = new string[numFoundDirs];
		for (unsigned int i = 0; i < numFoundDirs; i++)
			dirs[i] = foundDirs[i];
		delete foundDirs;

		return numFoundDirs;
	}

}

#endif 

Last edited on
Why are you not using a vector of strings?
Because I was not familair with it yet, know I searched for it and learned it. I replaced it with a vector and it works now, I still don't know why this code doesn't work but that's ok. I'll mark it as solved.
Topic archived. No new replies allowed.