| | |
Casting problem... (Win32)
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
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
>>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); Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
•
•
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
•
•
•
•
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
•
•
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 niek_e; Feb 8th, 2008 at 8:57 am.
•
•
•
•
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 niek_e; Feb 8th, 2008 at 9:36 am.
![]() |
Other Threads in the C++ Forum
- Previous Thread: keeping file from web browser cache
- Next Thread: detecting is boost::shared_ptr is NULL
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg simple sorting string strings temperature template text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






