How to put two backslash in C++

前端 未结 5 1536
北海茫月
北海茫月 2021-01-15 19:48

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

相关标签:
5条回答
  • 2021-01-15 19:50

    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;
    }
    
    0 讨论(0)
  • 2021-01-15 19:57

    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.

    0 讨论(0)
  • 2021-01-15 20:02

    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.

    0 讨论(0)
  • 2021-01-15 20:03

    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;
    } 
    
    0 讨论(0)
  • 2021-01-15 20:08

    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;
    }
    
    0 讨论(0)
提交回复
热议问题