note: the title may not be relevant to the issue, because I can't understand where the issue comes from.
The isuue: I have a piece of code which gets a filename from a dir and appends it to a vector < char* >
.
It works ok, but if I wrap it in a function, it gives weird behavior, the resulting vector element prints single random character in console instead of the filename.
I have checked and re-checked everything but can't see why it is happening.
Below is complete runnable code, I compiled it on Windows with cl.exe
namely just cl.exe "a.cpp" /EHsc
. x64 native environment and target.
I put together both pieces, for easier testing.
#include <windows.h>
#include <iostream>
#include <vector>
using namespace std;
void getimglist ( const char* mask, vector < char* > & flist )
{
WIN32_FIND_DATA data;
HANDLE hFind;
hFind = FindFirstFile ( mask, & data );
cout << "-fname: " << data.cFileName << "\n";
flist.push_back ( data.cFileName );
FindClose ( hFind );
}
int main (int argc, char* argv[])
{
vector < char* > L ;
vector < char* > L2 ;
const char* mask = ".\\input-cam0\\*.jpg";
const char* mask2 = ".\\input-cam0\\*.jpg";
// this code works
WIN32_FIND_DATA data;
HANDLE hFind;
hFind = FindFirstFile ( mask, & data );
cout << " first file: " << data.cFileName << "\n";
L.push_back ( data.cFileName );
FindClose (hFind);
// ***
cout << " size:" << L.size() << "\n";
cout << " first file L:" << L[0] << "\n";
// this works weird, output is different and wrong
cout << " ** function call **\n";
getimglist ( mask2, L2 );
cout << " size:" << L2.size() << "\n";
cout << " first file: " << L2[0] << "\n";
return 0;
} // end main
Output:
first file: 000-001.jpg
size:1
first file L:000-001.jpg
** function call **
-fname: 000-001.jpg
size:1
first file: R
See the last line ^, here it is R
, and if I run multiple times the exe file, it prints single random character. And the result from main
block gives correct results. Where is the problem?