| | |
Casting problem... (Win32)
![]() |
•
•
Join Date: Jan 2008
Posts: 5
Reputation:
Solved Threads: 0
heyya, sorry if this is the wrong forum for this... but i can hardly be blamed =p...
anyway, my line of code that gives me problem is this
str[0] =ffd.cFileName;
where str is an array of strings, and ffd is of WIN32_FIND_DATA type.
ffd.cFileName should be of type TCHAR.
my error says "...cannot convert 'CHAR[260]' to char..."
im not so great at Win32 development (as you may of guessed) so basically.
typing "cout << ffd.cFileName" gives:-
New Text Document.txt
...in the console, i want to store "New Text Document.txt" (ffd.cFileName) into the first element of an array of strings (str).
how can i do this?
thanks alot D.R
(p.s. i hope i explained this well, if not feel free to reply saying so =])
anyway, my line of code that gives me problem is this
str[0] =ffd.cFileName;
where str is an array of strings, and ffd is of WIN32_FIND_DATA type.
ffd.cFileName should be of type TCHAR.
my error says "...cannot convert 'CHAR[260]' to char..."
im not so great at Win32 development (as you may of guessed) so basically.
typing "cout << ffd.cFileName" gives:-
New Text Document.txt
...in the console, i want to store "New Text Document.txt" (ffd.cFileName) into the first element of an array of strings (str).
how can i do this?
thanks alot D.R
(p.s. i hope i explained this well, if not feel free to reply saying so =])
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:
Let me know if it works
Niek
so : string str[x] or char str[x]?
From w_char to string:
c Syntax (Toggle Plain Text)
#include <sstream> [....] stringstream ss; ss << ffd.cFileName; string str; ss >> str;
Niek
Show your C++ skills!
>>str[0] =ffd.cFileName;
It depends on how str was declared. Is it
It also depends on whether the program is being compiled with UNICODE or not. If you declared str using TCHAR then you could call _tstrcpy() and let the compiler figure out how to make the conversion.
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 declared str using TCHAR then you could call _tstrcpy() and let the compiler figure out how to make the conversion.
_tstrcpy( str, ffd.cFileName); The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
•
•
•
•
Originally Posted by Ancient Dragon
It depends on how str was declared. Is it char str[ <some number here]
or like this: TCHAR str[ <some number here>]
- strings
- char
- TCHAR
??
If the first: use my option, if the second or third use AD 's
Niek
Show your C++ skills!
•
•
•
•
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:
Let me know if it worksc Syntax (Toggle Plain Text)
#include <sstream> [....] stringstream ss; ss << ffd.cFileName; string str; ss >> str;
Niek
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by Ancient Dragon; Feb 8th, 2008 at 8:27 am.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
•
•
•
•
That doesn't work when the program is compiled for UNICODE. It will compile, but just produces garbage at runtime.
And the OP asked to convert ffd.cFileName (ffd = a WIN32_FIND_DATA type)
Niek
ps. #include <iostream> when using cout
Last edited by Nick Evan; Feb 8th, 2008 at 8:57 am.
Show your C++ skills!
>>ps. #include <iostream> 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.
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.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
•
•
•
•
There are just too many ifs about the OPs problem for us to be of much help.
@ OP:
I've done some more research and this code may or may not solve your problem, it works fine for me:
c Syntax (Toggle Plain Text)
#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)
Niek
Last edited by Nick Evan; Feb 8th, 2008 at 9:36 am.
Show your C++ skills!
![]() |
Other Threads in the C++ Forum
- Previous Thread: keeping file from web browser cache
- Next Thread: detecting is boost::shared_ptr is NULL
Views: 1641 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm api array arrays assignment beginner binary c++ c/c++ calculator char class classes client code command compile compiler constructor conversion convert count delete display dll dynamic encryption error file files fstream function functions game givemetehcodez graph gui homework iamthwee ifstream input int java lazy link linker loop looping loops math matrix member memory multidimensional newbie number numbers object objects operator output pointer pointers problem program programming project qt random read recursion recursive reference search simple sort spoonfeeding string strings struct student studio template templates text time tree undefined variable vc++ vector video visual void win32 window windows winsock wordfrequency wxwidgets






