Hi there,

I am new to C++ programming. What I want to do is, after I browse for a file using openFileDialog and after transforming the path from a System::String into a Char * what I want to do is to use the file`s path to open that file, but with simple \ between the root folders C++ doesn`t let me do that so I want to add another \ after every existing \ from the example path:C:\Users\NINJAH\Documents\Visual Studio 2008\Projects\myfile.txt so it will become C:\\Users\\NINJAH\\Documents\\Visual Studio 2008\\Projects\myfile.txt.
What I`ve tried is:

for (int i=0;i!=sizeof(nstring);i++){
     if (nstring[i]= '\' )
	 nstring[i]=nstring[i]+ "\";
 }

but it doesn`t work.

Please help!

Recommended Answers

All 10 Replies

You don't understand the use of the double \\ in strings. The \ chacter in C/C++ is used as an escape sequence and tells the compiler to treat the following character differently from other characters. For example '\n' is a newline, '\t' is a tab (there are several others), and '\\' tells the compiler to treat \ as a character literal instead of as an escape characters.

That is all unnecessary when you have a character array that already contains \ charcters in a path. OpenFileDialog() will return a string that already contains \ folder seperator characters so it is not necessary to replace each \ with \\.

>> but with simple \ between the root folders C++ doesn`t let me do that
There has to be something else you have done wrong because single \ works for everyone else in the world. My guess is that when you add the file name to the path you didn't put a \ before the file name.

I don`t uderstand how it is possible because I`ve tried to open a file as follows:

ifstream infile("C:\Users\NINJAH\Documents\Visual Studio 2008\Projects\myfile.txt");

but it does not work.

The thing is that I`ve tried to put double \ characters and it worked..no errors it reads from the file.

And when I use the openFileDialog and I take the path to the file..it gives the path like

C:\Users\NINJAH\Documents\Visual Studio 2008\Projects\myfile.txt

and not like

C:\\Users\\NINJAH\\Documents\\Visual Studio 2008\\Projects\\myfile.txt

In order to get a \ character into a string, or to write one as a char literal, you need to put two consecutive \ characters as part of your program between quotes. Examples:

char c;
c = '\';                 // Wrong.
c = '\\';                // Right -- c now contains a (single) backslash.

char* s;
s = "a\b\c";             // Wrong.
s = "a\\b\\c";           // Right -- s now points to the initial char of a
                         // 6-char array.  The six chars are a, backslash, b,
                         // backslash, c, null.

I don`t uderstand how it is possible because I`ve tried to open a file as follows:

ifstream infile("C:\Users\NINJAH\Documents\Visual Studio 2008\Projects\myfile.txt");

but it does not work.

The thing is that I`ve tried to put double \ characters and it worked..no errors it reads from the file.

And when I use the openFileDialog and I take the path to the file..it gives the path like

C:\Users\NINJAH\Documents\Visual Studio 2008\Projects\myfile.txt

and not like

C:\\Users\\NINJAH\\Documents\\Visual Studio 2008\\Projects\\myfile.txt

Your infile doesn't work because you didn't escape the backslashes. If you output the string "C:\Users\NINJAH\Documents\Visual Studio 2008\Projects\myfile.txt" instead of using it in an open command, this would be your output:

C:UsersNINJAHDocumentsVisual Studio 2008Projectsmyfile.txt

Notice how all of your backslashes disappeared and you have an extremely stupid-looking filename... This happened because it thought you were trying to escape the U, N, D, V, P, and m; none of which are valid escape sequences.

To prevent these errant attempts at creating escape sequences, you must escape the backslash. To do this, you enter '\\' istead of simply '\' into literals. Once you have the proper value into a variable the system automatically takes care of the escaping for you, you don't need to do anything more.

Ok. I have understood what you are trying to explain..and I thank you for the explanations..but still, I didnt manage to add to a char* another \ character after the one that it occurs in it. Ive used the following code:

 char* nstring="C:\Program Files\C++\myfile.txt";

 for (int i=0;i!=sizeof(nstring);i++){
    if (nstring[i]== ' \\ ' )
     nstring[i]<< nstring[i]+ "\\";
     }

what is it wrong?

In the end What I want to obtain for nstring is:

nstring="C:\\Program Files\\C++\\myfile.txt"

so I can use it later in finding my txt file.

How do I do that?

I don't think you read either of the last two messages in this thread. Because if you did, you would understand why the very first statement in your example:

char* nstring="C:\Program Files\C++\myfile.txt";

simply does not work.

The rest of your code does not work either. The statement

nstring << nstring + "\\";

is just plain nonsense. If you're writing code like this, it suggests that you don't understand anything about how C++ expressions work.

I don't mean to sound unkind, but I think you should spend some time studying a good C++ textbook before asking other people for advice. It is very inefficient to try to learn by asking questions about material you don't understand in the first place.

>>I have understood what you are trying to explain..
Clearly you didn't. If you had, you wouldn't still be trying to do this....

The simple fact of the matter is that you need to fix your literal, not re-work the array after the fact. By that point, it's already completely FUBARRED and it can't be salvaged by any sort of computation or substitution. Attempting to do so will only make the issue worse.

for (int i=0;i!=sizeof(nstring);i++){
if (nstring== ' \\ ' )
nstring<< nstring+ "\\";
}

what is it wrong?

sizeof(nstring) is not the same as strlen(nstring). Since nstring is a pointer, sizeof(nstring) is the same as sizeof(any pointer here) which is always the same value -- size of a pointer. For 32-bit compilers that will be 4. Don't confuse the difference of sizeof() operator and strlen() function because they are not interchangable.

>>char* nstring="C:\Program Files\C++\myfile.txt";

When working with string literals you have to use two \\ characters, like this: char* nstring="C:\\Program Files\\C++\\myfile.txt"; The two \\ are needed when the text is enclosed in quotes, like that statement.

sizeof(nstring) is not the same as strlen(nstring). Since nstring is a pointer, sizeof(nstring) is the same as sizeof(any pointer here) which is always the same value -- size of a pointer. For 32-bit compilers that will be 4. Don't confuse the difference of sizeof() operator and strlen() function because they are not interchangable.

>>char* nstring="C:\Program Files\C++\myfile.txt";

When working with string literals you have to use two \\ characters, like this: char* nstring="C:\\Program Files\\C++\\myfile.txt"; The two \\ are needed when the text is enclosed in quotes, like that statement.

I am sorry if I pissed someone, and I thank everybody for their help but I don`t but the path to my file with a single \, the openFileDialog from Windows forms retrives it into a variable with single \, that is why I want to add one more \ after each occurance, not because I didn`t understood that with a single \ it doesn`t work.

commented: You need to learn how to listen. -1

Stop arguing and start listening.

The string you get back from the openFileDialog is already formatted correctly. You don't need to do anything with it. The only time you need a double backslash is when you have a string literal.

string someString = "I am a string literal".

Your issue is somewhere else. Like AD implied in his first post, most likely you're not concatenating something correctly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.