simple(?) CHAR problems

heyya, i'm presuming that this is to do with win32.

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 windows 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" into the first element of an array of strings.

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 =])
Last edited on
(CHAR[260]) is a different type than (char*). However, you shouldn't just copy the pointer values, but you should instead copy the entire array (string).
1
2
3
4
#include <cstring>
...
strncpy( str[ 0 ], (const char *)ffd.cFileName, sizeof( str[ 0 ] ) -1 );
str[ 0 ][ sizeof( str[ 0 ] ) -1 ] = '\0';

Presumably the char array in str[0] is at minimum 260 chars long...
If you want to keep a list strings, all of which originally appear in ffd.cFileName, then you cannot just copy the pointer, since it will not remain valid. You must copy the string data. I'm assuming you're programming in C.
1
2
3
4
5
6
7
#include <stdlib.h>
#include <string.h>
...
  size_t len;
  len = strlen( ffd.cFileName );
  str[i] = malloc( len + 1 );
  strncpy( str[i], ffd.cFileName, len + 1 );

You just have to remember to loop through the array and free the memory at some point.
Topic archived. No new replies allowed.