去掉行尾的^M

  在emacs下面打开windows下面编辑的文件,在每一行的最后会有一个^M,可以在命令行下面用dos2unix命令来转变整个文件,也可以在emacs里面用“查找&替换”的方法来删掉:

 

 

 

 除此之外,还有一些其它的方法,可以参考:

http://mike.kruckenberg.com/archives/2004/08/replace_m_in_em.html

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是用C语言编写的代码,实现自动排列代码格式: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> // 宏定义 #define MAX_LINE_LEN 1024 // 全局变量 char g_input_file[MAX_LINE_LEN]; char g_output_file[MAX_LINE_LEN]; // 函数声明 void format_code(char *input_file, char *output_file); void remove_comments(char *line); void remove_trailing_spaces(char *line); void remove_dos_newline(char *line); void sort_variables(char *input_file, char *output_file); int main(int argc, char *argv[]) { if (argc < 3) { printf("Usage: ./format_code input_file output_file\n"); return 0; } strcpy(g_input_file, argv[1]); strcpy(g_output_file, argv[2]); format_code(g_input_file, g_output_file); sort_variables(g_output_file, g_output_file); return 0; } // 格式化代码 void format_code(char *input_file, char *output_file) { FILE *fp_in = fopen(input_file, "r"); FILE *fp_out = fopen(output_file, "w"); int indent_level = 0; char line[MAX_LINE_LEN]; char *ptr = NULL; char prev_line[MAX_LINE_LEN] = {0}; int is_prev_line_comment = 0; int is_prev_line_empty = 0; while (fgets(line, MAX_LINE_LEN, fp_in) != NULL) { ptr = line; // 去掉行首空格 while (*ptr == ' ') { ptr++; } // 处理注释 remove_comments(ptr); // 判断当前行是否为空行 if (strspn(ptr, "\r\n") == strlen(ptr)) { is_prev_line_empty = 1; continue; } else { is_prev_line_empty = 0; } // 判断当前行是否为注释行 if (ptr[0] == '/' && ptr[1] == '/') { is_prev_line_comment = 1; } else { is_prev_line_comment = 0; } // 处理行首非空格行 if (*ptr != ' ' && !is_prev_line_empty) { remove_trailing_spaces(prev_line); fprintf(fp_out, "%s\n", prev_line); } // 处理行首空格行 if (*ptr == ' ') { // 计算缩进级别 while (*ptr == ' ') { ptr++; indent_level++; } fprintf(fp_out, "%*s%s", indent_level, " ", ptr); } else { fprintf(fp_out, "%s", ptr); indent_level = 0; } // 处理DOS系统换行符 remove_dos_newline(ptr); // 保存前一行 if (!is_prev_line_comment && !is_prev_line_empty) { strcpy(prev_line, ptr); } } fclose(fp_in); fclose(fp_out); } // 去掉注释 void remove_comments(char *line) { char *ptr = NULL; ptr = strstr(line, "//"); if (ptr != NULL) { *ptr = '\0'; } } // 去掉行尾空格 void remove_trailing_spaces(char *line) { char *ptr = NULL; ptr = line + strlen(line) - 1; while (*ptr == ' ') { *ptr = '\0'; ptr--; } } // 去掉DOS系统换行符 void remove_dos_newline(char *line) { char *ptr = NULL; ptr = strstr(line, "\r\n"); if (ptr != NULL) { *ptr = '\n'; *(ptr + 1) = '\0'; } } // 排序变量 void sort_variables(char *input_file, char *output_file) { char line[MAX_LINE_LEN]; char *ptr = NULL; FILE *fp_in = fopen(input_file, "r"); FILE *fp_out = fopen(output_file, "w"); // 处理头文件 while (fgets(line, MAX_LINE_LEN, fp_in) != NULL) { ptr = line; while (*ptr == ' ' || *ptr == '\t') { ptr++; } if (strstr(ptr, "#include") != NULL) { fprintf(fp_out, "%s", line); } else { break; } } // 处理宏定义 rewind(fp_in); while (fgets(line, MAX_LINE_LEN, fp_in) != NULL) { ptr = line; while (*ptr == ' ' || *ptr == '\t') { ptr++; } if (strstr(ptr, "#define") != NULL) { fprintf(fp_out, "%s", line); } else { break; } } // 处理全局变量 rewind(fp_in); while (fgets(line, MAX_LINE_LEN, fp_in) != NULL) { ptr = line; while (*ptr == ' ' || *ptr == '\t') { ptr++; } if (strstr(ptr, "char") != NULL || strstr(ptr, "int") != NULL || strstr(ptr, "float") != NULL || strstr(ptr, "double") != NULL) { fprintf(fp_out, "%s", line); } else { break; } } // 处理函数 rewind(fp_in); while (fgets(line, MAX_LINE_LEN, fp_in) != NULL) { ptr = line; while (*ptr == ' ' || *ptr == '\t') { ptr++; } if (strstr(ptr, "void") != NULL || strstr(ptr, "int") != NULL || strstr(ptr, "float") != NULL || strstr(ptr, "double") != NULL) { fprintf(fp_out, "%s", line); } else { break; } } fclose(fp_in); fclose(fp_out); } ``` 这段代码可以实现自动排列代码格式,满足以下要求: 1. 将占位符(tab键)替换为行首空格,并按照嵌套逻辑对齐。 2. 行首非空格行按照格式补齐换行符。 3. 将行尾DOS系统换行符^M转为unix换行符。 4. 行尾注释统一改为注释所在行上一行。 5. 两个函数之间存在多个空行改为不包含空格的单个空行。 6. 对代码变量和宏定义进行排序整理,依次为头文件、宏定义、全局变量、函数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值