How do I place a backslash in a string?

 
I need to append the folder path and the filename of a file together to form the total filepath to be fed into a function. However I cannot seem to set a string equal to "C:\" for example, nor can I do "\" and then just append the three together. I keep getting the error message "double quotes needed". What is a double quote? I have never come across it before.
 

I think you should try for instance:

"c:\\metatrader\\experts\\files\\interest_rates.csv"

or

"c:/metatrader/experts/......"

 

Correct.

@whitebloodcell - The reason is that the backslash character has a special purpose in the context of a string. It says - treat the following character differently to normal.

Lets take the string abc.

string mystring = "abc";

The double quotes are used to mark the beginning and end of the string.

Now let's suppose we want the string to contain a"bc then:

string mystring = "a"bc";

Wouldn't work as the compiler would not be able to tell which set of double quotes ended the string.

So we use the backslash to say - treat the next character differently to what you would normally treat it.

Hence we declare:

string mystring = "a\"bc";

However if we have the following string:

string mystring = "directory\filename";

The compiler will think - ah here's a backslash, so the f must be treated differently to normal, therefore, as a special character. Obviously this won't work.

So we use the backslash before the backslash to tell the compiler to treat the second backslash differently than normal - ie. treat it not as a special character to alter the next character, but as an ordinary backslash.

string mystring = "directory\\filename";


Here's the relevant entry in the documentation.

https://docs.mql4.com/basis/types/literal


PS: can't believe you'd never heard of double quotes - you only used 6 of the damn things in your original question!!


CB

 
I have always referred to them as speech marks....(but I did make the connection here however). I interpreted it as wanting double, double quotes. i.e. ""x\"" which didn't work. "x\"" however does....
Reason: