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

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

Edited 5 Years Ago by Nick Evan: n/a

>>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);

...where str is an array of strings...

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

Edited 5 Years Ago by Nick Evan: n/a

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.

Edited 5 Years Ago by Nick Evan: n/a

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

Edited 5 Years Ago by Nick Evan: n/a

>>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.

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)

Edited 5 Years Ago by Nick Evan: n/a

This article has been dead for over six months. Start a new discussion instead.