Casting problem... (Win32)

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 5
Reputation: deadrabit is an unknown quantity at this point 
Solved Threads: 0
deadrabit deadrabit is offline Offline
Newbie Poster

Casting problem... (Win32)

 
0
  #1
Feb 8th, 2008
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 =])
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,916
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 304
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Casting problem... (Win32)

 
0
  #2
Feb 8th, 2008
Originally Posted by deadrabit View Post
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:
  1. #include <sstream>
  2.  
  3. [....]
  4. stringstream ss;
  5. ss << ffd.cFileName;
  6. string str;
  7. ss >> str;
Let me know if it works

Niek
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,484
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Casting problem... (Win32)

 
0
  #3
Feb 8th, 2008
>>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 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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,916
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 304
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Casting problem... (Win32)

 
0
  #4
Feb 8th, 2008
Originally Posted by deadrabit View Post
...where str is an array of strings...
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>]
Ok... So is str an array of

- strings
- char
- TCHAR
??

If the first: use my option, if the second or third use AD 's

Niek
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,484
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Casting problem... (Win32)

 
0
  #5
Feb 8th, 2008
Originally Posted by niek_e View Post
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:
  1. #include <sstream>
  2.  
  3. [....]
  4. stringstream ss;
  5. ss << ffd.cFileName;
  6. string str;
  7. 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.
  1. #include <sstream>
  2. #include <string>
  3. #include <windows.h>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. TCHAR cFileName[] = TEXT("Hello World");
  9. stringstream ss;
  10. ss << cFileName;
  11. string str;
  12. ss >> str;
  13.  
  14. cout << str << "\n";
  15. return 0;
  16. }
Now compile and run the above with UNICODE enabled.
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,916
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 304
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Casting problem... (Win32)

 
0
  #6
Feb 8th, 2008
Originally Posted by Ancient Dragon View Post
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)

Niek

ps. #include <iostream> when using cout
Last edited by niek_e; Feb 8th, 2008 at 8:57 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,484
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Casting problem... (Win32)

 
0
  #7
Feb 8th, 2008
>>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.
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,916
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 304
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Casting problem... (Win32)

 
0
  #8
Feb 8th, 2008
Originally Posted by Ancient Dragon View Post
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:
  1. #include "stdafx.h"
  2. #include <windows.h>
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int main ()
  9. {
  10. LPSTR str;
  11. WIN32_FIND_DATA ffd;
  12.  
  13. HANDLE hFind = FindFirstFile(L"c:\\windows", &ffd);
  14. if (hFind == INVALID_HANDLE_VALUE)
  15. {
  16. cout << "failed" << endl;
  17. return 0;
  18. }
  19.  
  20. _tprintf (TEXT("The first file found is %s\n"), ffd.cFileName);
  21. WideCharToMultiByte(0,WC_NO_BEST_FIT_CHARS,ffd.cFileName,-1,str,100,NULL,NULL);
  22.  
  23. cout << "The first file found is (as LPSTR)" << str << "\n";
  24.  
  25. cin.get();
  26. return 0;
  27. }

(for max 100 chars)
Niek
Last edited by niek_e; Feb 8th, 2008 at 9:36 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC