943,949 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1975
  • C++ RSS
Feb 8th, 2008
0

Casting problem... (Win32)

Expand Post »
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 =])
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deadrabit is offline Offline
5 posts
since Jan 2008
Feb 8th, 2008
0

Re: Casting problem... (Win32)

Click to Expand / Collapse  Quote originally posted by deadrabit ...
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
Last edited by Nick Evan; Jan 23rd, 2011 at 3:54 pm.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Feb 8th, 2008
0

Re: Casting problem... (Win32)

>>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);
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Feb 8th, 2008
0

Re: Casting problem... (Win32)

Click to Expand / Collapse  Quote originally posted by deadrabit ...
...where str is an array of strings...
Quote 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
Last edited by Nick Evan; Jan 23rd, 2011 at 3:55 pm.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Feb 8th, 2008
0

Re: Casting problem... (Win32)

Quote ...
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.
C++ Syntax (Toggle Plain Text)
  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 Nick Evan; Jan 23rd, 2011 at 3:55 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Feb 8th, 2008
0

Re: Casting problem... (Win32)

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 <iostream> when using cout
Last edited by Nick Evan; Jan 23rd, 2011 at 3:55 pm.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Feb 8th, 2008
0

Re: Casting problem... (Win32)

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Feb 8th, 2008
0

Re: Casting problem... (Win32)

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)
Last edited by Nick Evan; Jan 23rd, 2011 at 3:55 pm.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: keeping file from web browser cache
Next Thread in C++ Forum Timeline: detecting is boost::shared_ptr is NULL





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC