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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM