消除^M

來源:互聯網
上載者:User

標籤:des   style   blog   color   io   os   使用   ar   檔案   

在Linux和windows之間移動檔案時,總是會出現在windows下編輯的檔案在Linux開啟時每一行都顯示一個^M,雖然不影響使用,但是卻影響美觀,於是就想自己實現個小程式來進行轉換。

要實現轉換首先要弄明白為什麼出現上面的情況,詳細說是因為作業系統的曆史原因,簡單說就是windows的行結尾符是斷行符號+換行,而Linux是換行,所以windows下的檔案在Linux下會把行尾那個斷行符號顯示成^M這個符號。

瞭解了問題的原因,下面就是代碼,思路簡單,就不解釋了。

 1 #include <sys/time.h> 2 #include <iostream> 3 #include <fstream> 4 #include <string> 5 #include <string.h> 6  7 using namespace std; 8  9 void usage()10 {11     cout << "Please input like this:" << endl;12     cout << "WinToLinux oldfile newfile" << endl;13 }14 15 int main(int argc, char **argv)16 {17     unsigned int timeuse;18     struct timeval stStartTime;19     struct timeval stEndTime;20 21     usage();22 23     gettimeofday(&stStartTime, NULL);24 25     if(argc != 3)26     {27         cout << "Parameter is not deserved!" << endl;28         return -1;29     }30 31     ifstream fin(argv[1]);32     ofstream fout(argv[2]);33     34     if(!fin.is_open())35     {36         cout << "Open file failed,please check your file path." << endl;    37     }38     39     string strTmp;40     while(getline(fin, strTmp))41     {42         if(strTmp[strTmp.size() -1 ] == ‘\r‘)43         {44             strTmp.erase(strTmp.size() - 1, 1);45         }46         fout << strTmp << endl;47         strTmp.clear();48     }49 50     gettimeofday(&stEndTime, NULL);51     timeuse = 1000000*(stEndTime.tv_sec - stStartTime.tv_sec) + 52         stEndTime.tv_usec - stStartTime.tv_usec;53     cout << "Use time : " << timeuse/1000000.0 << " sec" << endl;54 55     return 0;56 }

編程要學以致用,有用的代碼才有價值

消除^M

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.