1

I am tryin to insert a \ charcter into a string so I would get

C:\\Users\\dave\\Desktop\\XY.txt instead of C:\Users\georg\Desktop\XY.txt

here is code but for some reason it does not work

string q="C:\Users\georg\Desktop\XY.txt";
char x='\\';


for (int i = 0; i < q.length(); ++i) {
if (q[i] == '\\')
 q.insert(i,4,x);
 }
georges
  • 11
  • 2
  • 1
    If you want really a string that contains `C:\\Users\\dave\\Desktop\\XY.txt`, you need to use `"C:\\\\Users\\\\dave\\\\Desktop\\\\XY.txt"`. – David Schwartz May 16 '16 at 19:21
  • 1
    Even if the string is correct, the `for` will have infinite insertions. – wally May 16 '16 at 19:33

4 Answers4

5

Just write

string q="C:\\Users\\georg\\Desktop\\XY.txt";

Or use a raw string literal

string q=R"(C:\Users\georg\Desktop\XY.txt)";
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • the thing is I do not declare the string this just a simple example the users clicks on a file and I get the string q="C:\Users\georg\Desktop\XY.txt" after that I would like to add the extra \ everywhere so I can open in my code the file that is referenced using .open(q) – georges May 16 '16 at 19:31
  • @georges There is no need to add the extra '\' character. The string already contains a valid file name. '\\' is an escape character that is converted to character '\' at the compile time while you are speaking about the run-time. – Vlad from Moscow May 16 '16 at 19:35
  • This does not work :string q=R"(C:\Users\georg\Desktop\XY.txt)"; myfile1.open(q); It only works with two \ ... – georges May 16 '16 at 19:37
  • We both got fooled by the first mistake Vlad. Seems there is a bigger mistake, you also should fix your answer. – Cem Kalyoncu May 16 '16 at 19:40
  • I just realized it was not working because I am using mcvs 2010 thank you however – georges May 16 '16 at 20:12
  • @georges Maybe instead of the string itself you should use q.c_str() to open the file. – Vlad from Moscow May 16 '16 at 20:20
  • @georges I think you should close this question and ask a new question that contains the relevant code. As for the original question then there is no any need to insert redundant '\' characters. You should elaborate what does not work. – Vlad from Moscow May 17 '16 at 19:10
2

You should use \ within quotes to add a single . So your first line is the problem. It should be

string q="C:\\Users\\georg\\Desktop\\XY.txt";

Then the rest will work.

EDIT

after additional explanation, I found problems with the follow up code. It should be fixed to the following:

for (int i = 0; i < q.length(); ++i) {
    if (q[i] == '\\') {
        q.insert(i,1,x);
        i++;
    }
}

Let me explain, first of all, you need only 1 additional \ so second parameter of insert should be 1. After inserting, you need to skip inserted \, hence i++;

Cem Kalyoncu
  • 14,120
  • 4
  • 40
  • 62
  • wouldn't this result in 5 backslashes in the string including the one that is already there? – Biruk Abebe May 16 '16 at 19:28
  • As you might know \ is escape character, so anything after \ might have an additional meaning \g is simply g and \\ is \ . – Cem Kalyoncu May 16 '16 at 19:31
  • he thing is I do not declare the string this just a simple example the users clicks on a file and I get the string q="C:\Users\georg\Desktop\XY.txt" after that I would like to add the extra \ everywhere so I can open in my code the file that is referenced using .open(q) – georges May 16 '16 at 19:33
  • sure i get that, but after applying your suggested edit, `q.insert(i,4,x)` will insert 4 backslashes just before the existing backslash making the count 5 right? – Biruk Abebe May 16 '16 at 19:33
  • I now noticed that, you are right and fixed my answer. – Cem Kalyoncu May 16 '16 at 19:37
  • @CemKalyoncu I downloaded a cpp compiler that supports R string however now I need to convert from string to R string to use your method since the user will provide a regular string in this format C:\Users\georg\Desktop\XY.txt. and I can't find anything online about that – georges May 17 '16 at 15:06
  • You don't need to perform anything on the user string. \\ is a requirement for your input within quotes. If you read your string from a file, a \ is a \ you don't need to perform any additional operations on it. If you give a more complete answer I might understand the issue better. – Cem Kalyoncu May 17 '16 at 18:00
1

Objective

Add an extra backslash to each one found in the string (for use in another program)

Current issues

  1. Example string is not escaped properly (easiest is to use a raw string)
  2. The for loop inserts 4 backslashes at a time and so will never reach the end.

Solution with C++11

This will add a backslash to each one that is already there:

#include <iostream>
#include <string>

int main()
{
    std::string q = R"(C:\Users\georg\Desktop\XY.txt)";

    char x='\\';


    for (int i = 0; i < q.length(); ++i) {
        if (q[i] == '\\') {
            q.insert(++i,1,x);
        }

    }
    return 0;
}

Solution without raw strings

#include <iostream>
#include <string>

int main()
{
    //std::string q = R"(C:\Users\georg\Desktop\XY.txt)";
    std::string q = "C:\\Users\\georg\\Desktop\\XY.txt";

    char x='\\';


    for (unsigned i = 0; i < q.length(); ++i) {
        if (q[i] == '\\') {
            q.insert(++i,1,x);
        }
    }

    std::cout << q;

    return 0;
}

Result

C:\\Users\\georg\\Desktop\\XY.txt
wally
  • 10,717
  • 5
  • 39
  • 72
  • it gives me undeclared identifier R even though I have the same copy pasted code – georges May 16 '16 at 19:42
  • @georges Raw string literal is a C++11 feature, may be your compiler doesn't support C++11. – Biruk Abebe May 16 '16 at 19:44
  • It works [here](http://coliru.stacked-crooked.com/a/2712fced837d2583), but I'll add a version that doesn't need the raw string. – wally May 16 '16 at 19:45
  • oh yeah It's because I am using mcvs 2010 as a compiler thank you – georges May 16 '16 at 19:50
  • @flatmouse I downloaded a cpp compiler that supports R string however now I need to convert from string to R string to use your method since the user will provide a regular string in this format C:\Users\georg\Desktop\XY.txt. and I can't find anything online about that – georges May 17 '16 at 15:07
  • @georges When the user provides the string you don't need to use `R` or raw strings. You only need it if you want the convenience in source code when typing the constant. If you accept user input into a string all the characters will be there as provided. If your program doesn't work as expected then post a new question with the code and examples of what does not work (sample input and output). – wally May 17 '16 at 16:38
0

The problem is that the first line either needs to be a raw string literal or have \\ in it:

#include <string>
#include <iostream>

using namespace std;

int main()
{
    string q  = R"(C:\Users\georg\Desktop\XY.txt)"; // Raw (C++11 and higher)
    string q2 = "C:\\Users\\georg\\Desktop\\XY.txt"; // Escaped

    cout << q << "\n"   // Outputs: C:\Users\georg\Desktop\XY.txt
         << q2 << "\n"; // Outpits: C:\Users\georg\Desktop\XY.txt
}

As written, it is trying to use \U, \g, etc. as special characters.


Update: To actually do what you are asking, you'd want to do something like:

#include <string>
#include <iostream>

using namespace std;

// From http://stackoverflow.com/a/15372760/201787
void replaceAll( string &s, const string &search, const string &replace ) {
    for( size_t pos = 0; ; pos += replace.length() ) {
        // Locate the substring to replace
        pos = s.find( search, pos );
        if( pos == string::npos ) break;
        // Replace by erasing and inserting
        s.erase( pos, search.length() );
        s.insert( pos, replace );
    }
}

int main()
{
    string q = R"(C:\Users\georg\Desktop\XY.txt)"; // Raw (C++11 and higher)
    string q2 = "C:\\Users\\georg\\Desktop\\XY.txt"; // Escaped
    string q3;
    std::cin >> q3; // Does not handle whitespace

    replaceAll( q, "\\", "\\\\" );
    replaceAll( q2, "\\", "\\\\" );
    replaceAll( q3, "\\", "\\\\" );

    cout << q << "\n"
         << q2 << "\n"
         << q3 << "\n";
}

Which prints the following if the user enters C:\Users\georg\Desktop\XY.txt :

C:\\Users\\georg\\Desktop\\XY.txt 
C:\\Users\\georg\\Desktop\\XY.txt
C:\\Users\\georg\\Desktop\\XY.txt
metal
  • 6,202
  • 1
  • 34
  • 49
  • That will give him the string `C:\Users\georg\Desktop\XY.txt`. But the question asked how to get `C:\\Users\\dave\\Desktop\\XY.txt`. – David Schwartz May 16 '16 at 19:22
  • wouldn't this result in 5 backslashes in the string including the one that is already there? – Biruk Abebe May 16 '16 at 19:27
  • 1
    Does that raw string really work? Shouldn't it be `std::string q = R"(C:\Users\georg\Desktop\XY.txt)"; // Raw`? – wally May 16 '16 at 19:29
  • he thing is I do not declare the string this just a simple example the users clicks on a file and I get the string q="C:\Users\georg\Desktop\XY.txt" after that I would like to add the extra \ everywhere so I can open in my code the file that is referenced using .open(q) – georges May 16 '16 at 19:33
  • do you think it's possible to do it with the same string as q i.e one \ only it won t be a raw string? – georges May 16 '16 at 20:11
  • @georges: Yes. See `q3` which I've added to the update. – metal May 17 '16 at 18:56