How to put two backslash in C++

前端 未结 5 1543
北海茫月
北海茫月 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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;
    }
    

提交回复
热议问题