Getting a char from one vector and adding to another

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
//Prototype
void getChars (std::vector<std::string> *chars);
void getFileNames(std::vector<std::string> *fileList);

int main();
{
   std::vector<std::string> characters;
   getChars (&characters)
   return 0;

}

//getChars
void getChars (std::vector<std::string> *chars)
{
   std::vector<std::string> listOfFiles;
   //Get the filenames
   getFileNames(&listOfFiles);

   chars->push_back (listOfFiles[0][1]); //I want to add the second character of the first file in listOfFiles but I get an error
//I can only get it to add the first element but not the second char within the first element.
}
   
//getFileNames Function
void getFileNames(std::vector<std::string> *fileList)
{
	WIN32_FIND_DATA search_data;
	
	memset(&search_data, 0, sizeof(WIN32_FIND_DATA));
	
	HANDLE handle = FindFirstFile("Files\\*.txt", &search_data); 
	
	while(handle != INVALID_HANDLE_VALUE)
	{
		fileList->push_back(std::string(search_data.cFileName));
		
		if(FindNextFile(handle, &search_data) == FALSE)
			break;
	}
}


See the comment on line 20, any ideas? cannot get the second character of the first element :( , writing the first element works fine though.
Last edited on
push.back Remind me why you're using the dot operator in a function name again? :P push_back is your fix... Simple mistake.
Last edited on
Passing the vector by ref would be better than passing it by pointer.

And you should call FindClose when you're finished with the handle returned by FindFirstFile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	HANDLE handle = FindFirstFile("Files\\*.txt", &search_data); 
	
	if(handle != INVALID_HANDLE_VALUE)
	{
		BOOL is_more = FALSE;

		do
		{
			fileList->push_back(std::string(search_data.cFileName));
		
			is_more = FindNextFile(handle, &search_data);
		}
		while(is_more);

		FindClose(handle); // close the handle
	}


Andy
Last edited on
Remind me why you're using the dot operator in a function name again? :P push_back is your fix... Simple mistake.


Sorry I was typing it out manually very quick before I had to rush out was just a typo and not a reflection of the actual code, which does not work. I have edited the code in the OP

Passing the vector by ref would be better than passing it by pointer.


Could you give an example please? I am not well versed with vectors at all, trying to get better with them obviously (not been programming long!) in theory I guess there should be no problem accessing a char from another vector ?
Last edited on
Could you give an example please?

Note that for in parameters you should use a const ref, but here as you're getting (it's an out parameter) you need a non-const ref.

Also, before you try to add a char to chars (line 20 of your orig code) you should check there is a first file (using vector<>::size or vector<>::empty).

Andy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//Prototype
void getChars (std::vector<std::string> &chars); // now a ref
void getFileNames(std::vector<std::string> &fileList); // now a ref

int main()
{
   std::vector<std::string> characters;
   getChars (characters); // don't use address-of operator
   return 0;
}

//getChars
void getChars (std::vector<std::string> &chars) // now a ref
{
   std::vector<std::string> listOfFiles;
   //Get the filenames
   getFileNames(listOfFiles); // don't use address-of operator

   chars.push_back (listOfFiles[0][1]); // now using . rather than -> for push_back
}

// etc 
Last edited on
Topic archived. No new replies allowed.