C++ 把文件路径中的单斜杠“\”换成双斜杠“\\”

 1 <pre name="code" class="cpp">
 2 #include <iostream>   
 4 #include <string>  
 5 using namespace std;   
 6    
 7    
 8 int main()  
 9 {  
10        string::size_type pos=0;  
11        string test="fsffsfd\\fdsfsfd\\fdsfsd";  
12        cout<<test<<endl;  
13    
14         while((pos=test.find('\\',pos))!=string::npos)  
15         {  
16             test.insert(pos,"\\");//插入  
17             pos=pos+2;  
18         }  
19     cout<<test<<endl;  
20    
21     return 0;  
22 }   

 注:

        C++中,将字符'\'用内部的转义字符‘\’来表示,所以在内部的'\'不能单独存在,如果存在内部的字符串中,则将其视为相连字符的转义字符,所以在内部要慎用‘\’字符。

     相反,外部传进来的字符'\',在C++内部自动转换为对应的转义字符'\\',开发者不需要考虑,知道怎么回事就行了。

转自:http://blog.csdn.net/huixingshao/article/details/46910761

参考:https://msdn.microsoft.com/en-us/library/syxtdd4f.aspx#basic_string::find_first_of

 

posted @ 2018-01-16 16:09  优秀afa  阅读(6870)  评论(0编辑  收藏  举报