wchar vs std::string

L

Lars Nielsen

Hey there

I have a win32 application written i c++. I have a std::vector of
std::string's i will fill with filenames.


typedef vector<std::string> strvector;
strvector vFiles;

WIN32_FIND_DATA fd;
HANDLE hFind = INVALID_HANDLE_VALUE;
hFind = FindFirstFile(szDataDir, &fd);
if(hFind == INVALID_HANDLE_VALUE)
{
return ERROR_INVALID_HANDLE;
}
else
{
while(FindNextFile(hFind, &fd) != 0)
{
if(!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
vFiles.push_back(fd.cFileName);
}
}
}


Since the cFileName is a WCHAR[260] array this code wont compile.
Can anyone tell how i can convert the wchar[] to a std::string, so I
can push it onto the vector?

Thanks

Lars
 
L

Larry I Smith

Lars said:
Hey there

I have a win32 application written i c++. I have a std::vector of
std::string's i will fill with filenames.


typedef vector<std::string> strvector;
strvector vFiles;

WIN32_FIND_DATA fd;
HANDLE hFind = INVALID_HANDLE_VALUE;
hFind = FindFirstFile(szDataDir, &fd);
if(hFind == INVALID_HANDLE_VALUE)
{
return ERROR_INVALID_HANDLE;
}
else
{
while(FindNextFile(hFind, &fd) != 0)
{
if(!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
vFiles.push_back(fd.cFileName);
}
}
}


Since the cFileName is a WCHAR[260] array this code wont compile.
Can anyone tell how i can convert the wchar[] to a std::string, so I
can push it onto the vector?

Thanks

Lars

Try

typedef vector<std::wstring> wstrvector;
wstrvector vFiles;

Regards,
Larry
 
V

Victor Bazarov

Lars said:
I have a win32 application written i c++. I have a std::vector of
std::string's i will fill with filenames.


typedef vector<std::string> strvector;
strvector vFiles;

WIN32_FIND_DATA fd;
HANDLE hFind = INVALID_HANDLE_VALUE;
hFind = FindFirstFile(szDataDir, &fd);
if(hFind == INVALID_HANDLE_VALUE)
{
return ERROR_INVALID_HANDLE;
}
else
{
while(FindNextFile(hFind, &fd) != 0)
{
if(!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
vFiles.push_back(fd.cFileName);
}
}
}


Since the cFileName is a WCHAR[260] array this code wont compile.
Can anyone tell how i can convert the wchar[] to a std::string, so I
can push it onto the vector?

Why don't you change your vector to

std::vector<std::wstring>

???

V
 
L

Lars Nielsen

Victor said:
Lars said:
I have a win32 application written i c++. I have a std::vector of
std::string's i will fill with filenames.


typedef vector<std::string> strvector;
strvector vFiles;

WIN32_FIND_DATA fd;
HANDLE hFind = INVALID_HANDLE_VALUE;
hFind = FindFirstFile(szDataDir, &fd);
if(hFind == INVALID_HANDLE_VALUE)
{
return ERROR_INVALID_HANDLE;
}
else
{
while(FindNextFile(hFind, &fd) != 0)
{
if(!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
vFiles.push_back(fd.cFileName);
}
}
}


Since the cFileName is a WCHAR[260] array this code wont compile.
Can anyone tell how i can convert the wchar[] to a std::string, so I
can push it onto the vector?


Why don't you change your vector to

std::vector<std::wstring>

???

V
Thanks!
I didnt think about the unicode issue! stupid me! ;)
It works fine with the wstring


/Lars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top