How to put two backslash in C++

13,171

Solution 1

In this line:

if(newpath[i] = '\\')

replace = with ==.

In this line:

newpath[i] +=  '\\';

This is supposed to add a \ into the string (I think that's what you want), but it actually does some funky char math on the current character. So instead of inserting a character, you are corrupting the data.

Try this instead:

 #include <iostream>
 #include <string>
 #include <sstream>

int main(int argc, char ** argv) {
  std::string a("hello\\ world");
  std::stringstream ss;

  for (int i = 0; i < a.length(); ++i) {
     if (a[i] == '\\') {
       ss << "\\\\";
     }
     else {
       ss << a[i];
     }
  }

  std::cout << ss.str() << std::endl;
  return 0;
}

Solution 2

int arrlength = sizeof(newpath); causes the size of your entire array (in chars) to be assigned to arrlength. This means you are iterating over 99999 characters in the array, even if the path is shorter (which it probably is).

Your loop condition also allows goes one past the bounds of the array (since the last (99999th) element is actually at index 99998, not 99999 -- arrays are zero-based):

for (int i = 0; newpath[i]] != '\0'; i++)

Also, there is no reason to copy the string into a character array first, when you can loop over the string object directly.

In any case, there is no need to escape backslashes from user input. The backslash is a single character like any other; it is only special when embedded in string literals in your code.

Solution 3

lots wrong. did not test this but it will get you closer http://www.cplusplus.com/reference/string/string/

string stripPath(string path)
{       
        string newpath;  
          for (int i = 0; i <= path.length() ;i++)
            {
                if(path.at(i) == '\\')
                {
                   newpath.append(path.at(i));
                   newpath.append(path.at(i));
                }
                else
                   newpath.append(path.at(i));
            }
        return newpath;
} 

Solution 4

The condition if (newpath[i] = '\\') should be if (newpath[i] == '\\').

The statement newpath[i] += '\\'; will not give the intended result of concatenation. It will instead add the integral value of '\\' to newpath[i].

Moreover why are you using a char newpath[99999]; array inside the function. newpath could be std::string newpath.

int main()
{
  std::string path = "c:\\test\\test2\\test3\\test4";

  std::cout << "orignal path: " << path << std::endl;

  size_t found = 0, next = 0;
  while( (found = path.find('\\', next)) != std::string::npos )
  {
    path.insert(found, "\\");
    next = found+4;
  }

  std::cout << "path with double slash: " << path << std::endl;

  return 0;
}

Solution 5

But in order for the compiler to read backslash in i need to create a function that will make a one backslash into 2 backslash

The compiler only reads string when you compile, and in that case you will need two as the first back slash will be an escape character. So if you were to have a static path string in code you would have to do something like this:

std::string path = "C:\\SomeFolder\\SomeTextFile.txt";

The compiler will never actually call your function only compile it. So writing a function like this so the compiler can read a string is not going to solve your problem.

Share:
13,171
Andro Miguel M. Bondoc
Author by

Andro Miguel M. Bondoc

Simple

Updated on June 05, 2022

Comments

  • Andro Miguel M. Bondoc
    Andro Miguel M. Bondoc almost 2 years

    i need to create a function that will accept a directory path. But in order for the compiler to read backslash in i need to create a function that will make a one backslash into 2 backslash.. so far this are my codes:

    string stripPath(string path)
    {       
            char newpath[99999];
            //char *pathlong;
            char temp;
            strcpy_s(newpath, path.c_str());
            //pathlong = newpath;
            int arrlength = sizeof(newpath);
    
                for (int i = 0; i <= arrlength ;i++)
                {
                    if(newpath[i] == '\\')
                    {
                        newpath[i] +=  '\\';
                        i++;
                    }
                }
                path = newpath;
            return path;
    } 
    

    this code receives an input from a user which is a directory path with single backslash. the problem is it gives a dirty text output;

  • phooji
    phooji about 13 years
    (I am probably jumping the gun here, since the question currently seems to end mid-sentence...)
  • Andro Miguel M. Bondoc
    Andro Miguel M. Bondoc about 13 years
    it still results a dirty text.
  • Andro Miguel M. Bondoc
    Andro Miguel M. Bondoc about 13 years
    it changes the backslash into a dirty text
  • phooji
    phooji about 13 years
    @Andro: I've include a quick example of how to do this using a stringstream instead of C-style string manipulation functions.
  • phooji
    phooji about 13 years
    +1 for finding a bug I initially overlooked. I think, between the two of us, we still haven't listed all the things that are wrong about this though :)
  • Cameron
    Cameron about 13 years
    @phooji: I stopped looking when I realized what the code was doing ;-)
  • phooji
    phooji about 13 years
    @Codeface: This is a very sceptical interpretation of the OP -- I like it. I guess I assumed there was some code generation going on here, e.g., this program needs to add escapes because the path is getting written to a file as a string literal.