• 消除^M


    在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 ] == '
    ')
    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 }

    编程要学以致用,有用的代码才有价值

    在用vim编辑时如果需要添加这个符号:^M,在插入状态输入ctrl+v+m即可。

  • 相关阅读:
    工作那些事(四)大公司VS小公司
    HTTP Keep-Alive详解
    Burp Suite Walkthrough(英文版)
    Burp Suite Walkthrough(中文版)
    Burp Suite教程(英文版)
    XSS之学习误区分析
    工具猫魔盒介绍
    XSS脚本攻击漫谈
    WEB黑客工具箱之FireBug介绍
    WEB黑客工具箱之LiveHttpHeaders介绍
  • 原文地址:https://www.cnblogs.com/lit10050528/p/3979583.html
Copyright © 2020-2023  润新知