删除windows代码文件中的'^M'
2011-12-13 23:05
316 查看
有时从windows中copy过来的代码文件中会有很多'^M'(回车)字符,
这使代码看起来很不整洁。这里分享一个简单的处理办法。
思路:
--
其他方法:
perl-> http://www.perlmonks.org/?node_id=183567
这使代码看起来很不整洁。这里分享一个简单的处理办法。
思路:
找到文件中的'^M'字符,并全部替换为space。最后用indent整理代码。 '^M'的ascii是13。代码:
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #define M_ASC 13 int main(int argc, char **argv) { char ch, *path; int res; FILE *f; long pos; if (argc > 1) path = argv[1]; f = fopen(path, "r+"); if (!f) { fprintf(stderr, "Open() %s ERROR(%s)\n", path, strerror(errno)); exit(EXIT_FAILURE); } do { pos = ftell(f); ch = fgetc(f); if (ch == M_ASC) { fseek(f, pos, SEEK_SET); fputc(' ', f); } } while (ch != EOF); fclose(f); exit(EXIT_SUCCESS); }用indent工具整理代码:
#!/bin/sh PARAM="-npro -kr -i8 -ts8 -sob -l80 -ss -ncs -cp1" indent $PARAM "$@"
--
其他方法:
perl-> http://www.perlmonks.org/?node_id=183567
#!/usr/bin/perl -w use strict; my $out; if(@ARGV!=1 && @ARGV!=2){ print "Usage:\n\t$0 input [output];\n"; exit; } open(IN, "<$ARGV[0]") or die "couldn't open $ARGV[0]: $!"; if(<IN>=~m/(^#!.*perl.*)/){ $out.=$1; } while(<IN>){ chomp; chop; $_.="\n"; $out.=$_; } close IN; if(@ARGV==1){ unlink $ARGV[0]; open(OUT,">$ARGV[0]") or die "couldn't open $ARGV[0]: $!"; } elsif(@ARGV==2){ open(OUT,">$ARGV[1]") or die "couldn't open $ARGV[1]: $!"; } print OUT $out; close OUT;
相关文章推荐
- find: missing argument to `-exec' & 删除指定时间的文件
- 'c:/windows/sytem32/ieframe.dll/1未找到'的解决方法
- 'mysql' 不是内部或外部命令,也不是可运行的程序或批处理文件的解决办法
- '步步为营'游戏代码(js+HTML)
- 由于文件组 'PRIMARY 中的磁盘空间不足,无法为数据库 'newnet' 分配新页。请删除文件组中的对象、将其他文件添加到文件组或者为文件组中的现有文件启用自动增长,以便增加必要的空间。
- 删除名为"-a"的文件
- "无法删除文件夹,目录不是空的"或U盘文件无法删除的解决办法
- 从外部的js文件中使用<%=%>获取ASPX页面的ClientID获取后台代码
- 'mysql' 不是内部或外部命令,也不是可运行的程序,或批处理文件。
- linux编辑或删除以'-'开头的文件
- Windows & Linux 文件格式之迷 < VI下删除文本中的^M>
- Windows & Linux 文件格式之迷 < VI下删除文本中的^M>
- 收藏 '分享 到百度、微信'功能代码
- 'javac' 不是内部或外部命令,也不是可运行的程序或批处理文件。
- 'adb' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
- 'adb' 不是内部或外部命令,也不是可运行的程序或批处理文件。
- 'mysql'不是内部或外部命令,也不是可运行的程序或批处理文件
- 'sqlplus'不是内部或外部命令,也不是可运行的程序或批处理文件
- 'cl' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
- 【node.js】windows安装express:'express' 不是内部或外部命令,也不是可运行的程序或批处理文件。