Double BackSlashes

M

Mikheil

Hello!
I need to translate file destination name with one backslashes
"c:\program files\directory\file.txt"
to string containing double backslashes
"c:\\program files\\directory\\file.txt"
If there is a nativi function on C++, or any algolithms I'd be glad
to know about them.
Thank you
 
C

Christoph Rabel

Mikheil said:
I need to translate file destination name with one backslashes
"c:\program files\directory\file.txt"
to string containing double backslashes
"c:\\program files\\directory\\file.txt"
If there is a nativi function on C++, or any algolithms I'd be glad
to know about them.

Why not translate it to
"c:/program files/directory/file.txt"?

This should be far easier to accomplish and works too on any
Windows OS. (ok, Im not sure about 3.11)

Otherwise you could use std::string.find and insert (or replace)

(untested):

std::string temp = "c:\program files\directory\file.txt";

for (int i = 0; i < temp.length(); ++i)
if (temp == '\\')
{
temp.insert(i, 1, '\\');
++i; // Skip inserted char
}


cya

Christoph
 
J

John Harrison

Mikheil said:
Hello!
I need to translate file destination name with one backslashes
"c:\program files\directory\file.txt"
to string containing double backslashes
"c:\\program files\\directory\\file.txt"
If there is a nativi function on C++, or any algolithms I'd be glad
to know about them.
Thank you

I think you are confused, a double backslash is just the way you write a
single backslash in C++.

"\\" - this string is a single backslash.

"\\\\" - this string is a double backslash.

If you have a string in memory with single backslashes then it will work to
open a file. If you have a string in your program code, then you *type*
double backslashes. There is no need to replace single backslashes with
double blackslashes programmatically.

john
 
J

John Harrison

Mikheil said:
Hello!
I need to translate file destination name with one backslashes
"c:\program files\directory\file.txt"

This string does not have any backslashes

\p, \d are illegal characters, \f is the form feed character.
to string containing double backslashes
"c:\\program files\\directory\\file.txt"

This string has three single backslashes, \\ is the way you write a single
backslash character. \\\\ is a double backslash character.
If there is a nativi function on C++, or any algolithms I'd be glad
to know about them.
Thank you

john
 
C

Christoph Rabel

Christoph said:
Hmm, I can think of at least one case where it is necessary to write
double backslashes out to a file.

Oops, misunderstood you. Of course it should be:

std::string temp = "c:\\program files\\directory\\file.txt";

thx for correcting me.

Christoph
 
C

Christoph Rabel

Andrey said:
This will work?

Probably. Depends on your needs.
Its most of the time better to use / then \. Far less troubles,
especially if you want to port the stuff one day.
What functions with? Will this work with stdlib?

I dont understand where your questions aim. Maybe you could try to
explain what you want to achieve. As John pointed out its most of the
time not necessary to change anything.

If \ or / works is OS specific, all std-functions only invoke the OS
functions and give them the paths. So, if the OS knows how to interpret
it, then it works.

mfg

Christoph
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top