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::str ing> strvector;
strvector vFiles;
WIN32_FIND_DATA fd;
HANDLE hFind = INVALID_HANDLE_ VALUE;
hFind = FindFirstFile(s zDataDir, &fd);
if(hFind == INVALID_HANDLE_ VALUE)
{
return ERROR_INVALID_H ANDLE;
}
else
{
while(FindNextF ile(hFind, &fd) != 0)
{
if(!(fd.dwFileA ttributes & FILE_ATTRIBUTE_ DIRECTORY))
{
vFiles.push_bac k(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 3 5510
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::str ing> strvector; strvector vFiles;
WIN32_FIND_DATA fd; HANDLE hFind = INVALID_HANDLE_ VALUE; hFind = FindFirstFile(s zDataDir, &fd); if(hFind == INVALID_HANDLE_ VALUE) { return ERROR_INVALID_H ANDLE; } else { while(FindNextF ile(hFind, &fd) != 0) { if(!(fd.dwFileA ttributes & FILE_ATTRIBUTE_ DIRECTORY)) { vFiles.push_bac k(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::wst ring> wstrvector;
wstrvector vFiles;
Regards,
Larry
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::str ing> strvector; strvector vFiles;
WIN32_FIND_DATA fd; HANDLE hFind = INVALID_HANDLE_ VALUE; hFind = FindFirstFile(s zDataDir, &fd); if(hFind == INVALID_HANDLE_ VALUE) { return ERROR_INVALID_H ANDLE; } else { while(FindNextF ile(hFind, &fd) != 0) { if(!(fd.dwFileA ttributes & FILE_ATTRIBUTE_ DIRECTORY)) { vFiles.push_bac k(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
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::str ing> strvector; strvector vFiles;
WIN32_FIND_DATA fd; HANDLE hFind = INVALID_HANDLE_ VALUE; hFind = FindFirstFile(s zDataDir, &fd); if(hFind == INVALID_HANDLE_ VALUE) { return ERROR_INVALID_H ANDLE; } else { while(FindNextF ile(hFind, &fd) != 0) { if(!(fd.dwFileA ttributes & FILE_ATTRIBUTE_ DIRECTORY)) { vFiles.push_bac k(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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 effectively impossible to forward declare
std::string. (Yes I am aware that some libraries have a string_fwd.h header,
but this is not portable.)
That said, is there any real reason why I can't derive an otherwise empty
|
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( (idx=str.find_first_of(',')) >= 0 ) {
str.replace( idx, 1, "" );
str.insert( idx, " or " );
}
|
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;
char c;
std::string str;
|
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 found anything).
Here's the problem, I have two sets of files, the name of a file contains a
number which is unique for each set but it's possible (even probable) that
two files in different sets have the same numbers. I want to store these...
|
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 a namespace and another
typedef to basic_string:
namespace my_string
{
| |
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 example
string str1; as a class member and then add text to it. or do I have
to
specify it's length when defining?
2. How to convert from std::string to LPCSTR
|
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;
|
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;
};
|
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 ""
// 'words' are any sequences of non-whitespace characters
// leading, trailing and multiple whitespace characters
// should be ignored.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |