• <bdo id='kDsPo'></bdo><ul id='kDsPo'></ul>
  • <tfoot id='kDsPo'></tfoot>
      <legend id='kDsPo'><style id='kDsPo'><dir id='kDsPo'><q id='kDsPo'></q></dir></style></legend>
    1. <small id='kDsPo'></small><noframes id='kDsPo'>

      1. <i id='kDsPo'><tr id='kDsPo'><dt id='kDsPo'><q id='kDsPo'><span id='kDsPo'><b id='kDsPo'><form id='kDsPo'><ins id='kDsPo'></ins><ul id='kDsPo'></ul><sub id='kDsPo'></sub></form><legend id='kDsPo'></legend><bdo id='kDsPo'><pre id='kDsPo'><center id='kDsPo'></center></pre></bdo></b><th id='kDsPo'></th></span></q></dt></tr></i><div id='kDsPo'><tfoot id='kDsPo'></tfoot><dl id='kDsPo'><fieldset id='kDsPo'></fieldset></dl></div>

        如何在C++中放置两个反斜杠

        时间:2023-10-18
        <legend id='wQTPk'><style id='wQTPk'><dir id='wQTPk'><q id='wQTPk'></q></dir></style></legend>

        <small id='wQTPk'></small><noframes id='wQTPk'>

          <tbody id='wQTPk'></tbody>

        • <tfoot id='wQTPk'></tfoot>

                <bdo id='wQTPk'></bdo><ul id='wQTPk'></ul>

                <i id='wQTPk'><tr id='wQTPk'><dt id='wQTPk'><q id='wQTPk'><span id='wQTPk'><b id='wQTPk'><form id='wQTPk'><ins id='wQTPk'></ins><ul id='wQTPk'></ul><sub id='wQTPk'></sub></form><legend id='wQTPk'></legend><bdo id='wQTPk'><pre id='wQTPk'><center id='wQTPk'></center></pre></bdo></b><th id='wQTPk'></th></span></q></dt></tr></i><div id='wQTPk'><tfoot id='wQTPk'></tfoot><dl id='wQTPk'><fieldset id='wQTPk'></fieldset></dl></div>
                  本文介绍了如何在C++中放置两个反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要创建一个接受目录路径的函数.但是为了让编译器读取反斜杠,我需要创建一个函数,将一个反斜杠变成 2 个反斜杠..到目前为止这是我的代码:

                  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;

                  推荐答案

                  在这一行:

                  if(newpath[i] = '\')
                  

                  = 替换为 ==.

                  在这一行:

                  newpath[i] +=  '\';
                  

                  这应该是将 添加到字符串中(我认为这就是您想要的),但它实际上对当前字符进行了一些时髦的 char 数学运算.因此,您不是插入字符,而是破坏了数据.

                  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.

                  试试这个:

                   #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;
                  }
                  

                  这篇关于如何在C++中放置两个反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:带有 desktop.ini &amp; 的自定义文件夹图标瞬间提神 下一篇:如何在 C++ 中正确删除链表的节点

                  相关文章

                1. <small id='W3G9v'></small><noframes id='W3G9v'>

                2. <legend id='W3G9v'><style id='W3G9v'><dir id='W3G9v'><q id='W3G9v'></q></dir></style></legend>
                  <tfoot id='W3G9v'></tfoot>

                  <i id='W3G9v'><tr id='W3G9v'><dt id='W3G9v'><q id='W3G9v'><span id='W3G9v'><b id='W3G9v'><form id='W3G9v'><ins id='W3G9v'></ins><ul id='W3G9v'></ul><sub id='W3G9v'></sub></form><legend id='W3G9v'></legend><bdo id='W3G9v'><pre id='W3G9v'><center id='W3G9v'></center></pre></bdo></b><th id='W3G9v'></th></span></q></dt></tr></i><div id='W3G9v'><tfoot id='W3G9v'></tfoot><dl id='W3G9v'><fieldset id='W3G9v'></fieldset></dl></div>

                    • <bdo id='W3G9v'></bdo><ul id='W3G9v'></ul>