473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 5482
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
8
by: Patrick Kowalzick | last post by:
Dear NG, I would like to change the allocator of e.g. all std::strings, without changing my code. Is there a portable solution to achieve this? The only nice solution I can think of, would be...
6
by: Nemok | last post by:
Hi, I am new to STD so I have some questions about std::string because I want use it in one of my projects instead of CString. 1. Is memory set dinamicaly (like CString), can I define for...
2
by: FBergemann | last post by:
if i compile following sample: #include <iostream> #include <string> int main(int argc, char **argv) { std::string test = "hallo9811111z"; std::string::size_type ret;
84
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.