954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Casting problem... (Win32)

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 =])

deadrabit
Newbie Poster
5 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 
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
Moderator
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
Team Colleague
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
Moderator
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
Team Colleague
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
Moderator
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
Team Colleague
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
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You