472,782 Members | 1,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 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 5435
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.