where str is an array of strings
Do you mean an array of strings or an array of chars = (sort of)string
so : string str[x] or char str[x]?
From w_char to string:
#include <sstream>
[....]
stringstream ss;
ss << ffd.cFileName;
string str;
ss >> str;
Let me know if it works
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
>>str[0] =ffd.cFileName;
It depends on how str was declared. Is it char str[ <some number here]
or like this: TCHAR str[ <some number here> ]
It also depends on whether the program is being compiled with UNICODE or not. If you declaredstr using TCHAR then you could call _tstrcpy () and let the compiler figure out how to make the conversion.
_tstrcpy( str, ffd.cFileName);
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
...where str is an array of strings...
It depends on how str was declared. Is it char str[ ]
Ok... So is str an array of
- strings
- char
- TCHAR
??
If the first: use my option, if the second or third use AD 's
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
Do you mean an array of strings or an array of chars = (sort of)string
so : string str[x] or char str[x]?
From w_char to string:
#include <sstream>
[....]
stringstream ss;
ss << ffd.cFileName;
string str;
ss >> str;
Let me know if it works
Niek
That doesn't work when the program is compiled for UNICODE. It will compile, but just produces garbage at runtime.
#include <sstream>
#include <string>
#include <windows.h>
using namespace std;
int main()
{
TCHAR cFileName[] = TEXT("Hello World");
stringstream ss;
ss << cFileName;
string str;
ss >> str;
cout << str << "\n";
return 0;
}
Now compile and run the above with UNICODE enabled.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
That doesn't work when the program is compiled for UNICODE. It will compile, but just produces garbage at runtime.
You're right, I've checked, but so does your code when UNICODE is enabled. It just prints the memoryaddress of the first element of the T_CHAR array.
And the OP asked to convert ffd.cFileName (ffd = a WIN32_FIND_DATA type)
ps. #include when using cout
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
>>ps. #include when using cout
You are right -- I forgot that I had it in stdafx.h
There are just too many ifs about the OPs problem for us to be of much help.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
There are just too many ifs about the OPs problem for us to be of much help.
You're right. If the OP just compiled without UNICODE, all his problems would magicaly dissappear.
@ OP:
I've done some more research and this code may or may not solve your problem, it works fine for me:
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <string>
using namespace std;
int main ()
{
LPSTR str;
WIN32_FIND_DATA ffd;
HANDLE hFind = FindFirstFile(L"c:\\windows", &ffd);
if (hFind == INVALID_HANDLE_VALUE)
{
cout << "failed" << endl;
return 0;
}
_tprintf (TEXT("The first file found is %s\n"), ffd.cFileName);
WideCharToMultiByte(0,WC_NO_BEST_FIT_CHARS,ffd.cFileName,-1,str,100,NULL,NULL);
cout << "The first file found is (as LPSTR)" << str << "\n";
cin.get();
return 0;
}
(for max 100 chars)
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403