468,752 Members | 1,385 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,752 developers. It's quick & easy.

wchar vs std::string

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
Jul 23 '05 #1
3 5204
Lars Nielsen wrote:
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
Jul 23 '05 #2
Lars Nielsen wrote:
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
Jul 23 '05 #3
Victor Bazarov wrote:
Lars Nielsen wrote:
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
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Angus Leeming | last post: by
11 posts views Thread by Christopher Benson-Manica | last post: by
22 posts views Thread by Jason Heyes | last post: by
19 posts views Thread by Erik Wikström | last post: by
8 posts views Thread by Patrick Kowalzick | last post: by
6 posts views Thread by Nemok | last post: by
84 posts views Thread by Peter Olcott | last post: by
11 posts views Thread by Jacek Dziedzic | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.