標籤: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