Sign in to follow this  
Ronnie TRFC

Problem adding a varible to file path

Recommended Posts

Ronnie TRFC    122
Hello, I'm having an issue when attempting to add a variable to a file path. I may not have explained that well so here is the small portion of code:
// I have created and intitialised the variable here like so. i have only intialised it here for the purpose of testing it

string FileChoice = "file1.txt";

// Further on in the code i have attempted to open the file like so
InFile.open( "C:\\Documents and Settings\\Owner\\My Documents\\ + FileChoice );

The error i get is quite long winded but it seems to state that i cannot convert from 'basic string' to 'const wchar*t'. i have never encountered a const wchar*t before and am quite confused because of it. Any help would be much appreciated and if you need me to supply any more code extracts, don't hesitate to ask. Tahnks in advance.

Share this post


Link to post
Share on other sites
Cornstalks    7030
Well, for starters, InFile.open( "C:\\Documents and Settings\\Owner\\My Documents\\ + FileChoice ); should be InFile.open( "C:\\Documents and Settings\\Owner\\My Documents\\" + FileChoice ); (you're missing a terminating " character).

Second of all, it looks like your strings are unicode (because wide characters are unicode). Just make sure you are using the string encoding throughout your project. If you have no idea what I'm talking about, then this probably isn't a problem for you, because if it was you would have encountered it before.

Third, how is InFile.open() defined? The error you are saying you are getting looks like you are trying to go from your string (FileChoice) to a const wchar*t (is there really an asterik before the t? I'm not too familiar with unicode and wide characters). Basically this means that you should try something like this

string FileChoice = "file1.txt";

FileChoice = "C:\\Documents and Settings\\Owner\\My Documents\\" + FileChoice;

InFile.open( FileChoice.c_str() );

Essentually, you need to convert from "string" to "const wchar*t". This is what the c_str() function does. I can't explain it in more detail, I have to go to bed. But here are some references you may want to look at

Reference
Reference 2 (see the section "Retrieving a char*", which is near the bottom)

(Disclaimer: I'm not going to guarantee the safety of the above code, as I haven't actually tried it, and especially since I don't usually work with wide characters)

Share this post


Link to post
Share on other sites
Ronnie TRFC    122
Thank you for the reply mate.

I havn't heard of alot of string encoding etc, therefore i presume that isn't the problem.

I will try your suggestion and hopefully have some luck.

Thanks and Sleep well mate.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this