convert char * to char []

T

T. Crane

Help!

I am trying to figure this out, and I have the impression that it
should be really easy, but I'm at a loss.

Here's my problem:

I want to take the output from

const char * string::c_str();

and convert it in some appropriate fasion so that I can use it as the
input to this function:

void Config(const char filename[]);

any help is greatly appreciated!

trevis
 
V

Victor Bazarov

T. Crane said:
I am trying to figure this out, and I have the impression that it
should be really easy, but I'm at a loss.

Here's my problem:

I want to take the output from

const char * string::c_str();

and convert it in some appropriate fasion so that I can use it as the
input to this function:

void Config(const char filename[]);

Just pass the call to your string's 'c_str' directly:

..blahblah...
Config(mysring.c_str());

The trick here is that the declaration of 'filename' argument is not
what it seems, and in fact 'filename' is just a pointer:

const char filename[] === const char *filename

V
 
T

T. Crane

T. Crane said:
I am trying to figure this out, and I have the impression that it
should be really easy, but I'm at a loss.
Here's my problem:
I want to take the output from
const char * string::c_str();
and convert it in some appropriate fasion so that I can use it as the
input to this function:
void Config(const char filename[]);

Just pass the call to your string's 'c_str' directly:

..blahblah...
Config(mysring.c_str());

The trick here is that the declaration of 'filename' argument is not
what it seems, and in fact 'filename' is just a pointer:

const char filename[] === const char *filename

V

Yeah, that's what I tried. For some reason it doesn't work. It
compiles fine, but something goes wrong after that, so I'm going to
have to debug it.

thanks,
trevis
 
F

Frank Birbacher

Hi!

T. Crane said:
Yeah, that's what I tried. For some reason it doesn't work. It
compiles fine, but something goes wrong after that, so I'm going to
have to debug it.

Note: the array becomes invalid when you change the string or when the
string goes out of scope (is destructed).

Frank
 
T

T. Crane

Hi!



Note: the array becomes invalid when you change the string or when the
string goes out of scope (is destructed).

Frank

So it seems that my problem is definitely in the Config function. The
filename parameter that is passed to Config() ends up going to another
function called CreateFile. CreateFile's first parameter is of the
type LPCTSTR, and there is some issue in coverting my const char * to
the LPCTSTR type. Something to do with Unicode. I dunno. Any help
understanding this is appreciated.

thanks,
trevis
 
C

Chris Thomasson

T. Crane said:
So it seems that my problem is definitely in the Config function. The
filename parameter that is passed to Config() ends up going to another
function called CreateFile. CreateFile's first parameter is of the
type LPCTSTR, and there is some issue in coverting my const char * to
the LPCTSTR type. Something to do with Unicode. I dunno. Any help
understanding this is appreciated.

CreateFile is macro that defines itself as CreateFileA for ansi, or
CreateFileW for unicode. I believe that you have to define UNICODE, or
something like that, in order for the windows.h header to use CreateFileW.
 
B

BobR

T. Crane said:
So it seems that my problem is definitely in the Config function. The
filename parameter that is passed to Config() ends up going to another
function called CreateFile. CreateFile's first parameter is of the
type LPCTSTR, and there is some issue in coverting my const char * to
the LPCTSTR type. Something to do with Unicode. I dunno. Any help
understanding this is appreciated.
thanks, trevis

If it's a wide-char, try 'std::wstring'.

There's also the 'vector-trick':

std::vector<TCHAR> MyString( 1024 ); // be sure it's big enough
LPCTSTR pwString = &MyString.at(0);

Try some tests to find out what you are using:

char Ch( 'A' );
wchar_t wCh( 'A' );
LPCTSTR pS = &Ch;
LPCTSTR pwS = &wCh;

Which one does not compile? (comment-out one at a time)

TCHAR dummy('A');
std::cout<<"sizeof(dummy)="<<sizeof(dummy)<<std::endl;
std::cout<<"sizeof(TCHAR)="<<sizeof(TCHAR)<<std::endl;

What is the output of those lines?

If 'Config()' is something you wrote, post it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top