T.P版在不··上次那个程序修改
程序代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define HELP 1 #define INDENT 2 FILE *fop; int handleargs(int argc, char* argv[]) { if (argc != 2){ return HELP; } if (argc == 2){ return INDENT; } } void printindent(int indent) { int i; for(i=0; i<indent; i++){ fprintf(fop, " "); } } void printchar(char* line) { int k =0; while(line[k++] == ' ') ; fputs(line+(k-1), fop); k = 0; } void printline(int indent, char* line) { printindent(indent); printchar(line); } void doIdent(char* filename) { int i = 0; int k = 0; char line[1000]; int indent = 0; FILE *fip =fopen(filename, "r"); fop = fopen("a.tmp", "w"); fgets(line, 1000, fip); while (!ferror(fip)&&!feof(fip)){ printf("%s",line); printf("%s", line); if(line[strlen(line)-2] == '}'){ indent -=2; printline(indent, line); } else if(line[strlen(line)-2] == '{'){ printline(indent, line); indent +=2; } else if(line[strlen(line)-1] == '}'){ indent -=2; printline(indent, line); } else printline(indent,line); fgets(line, 1000, fip); } fclose(fip); } void help() { printf("Useage: a filename /* The document indented */"); } int main(int argc, char* argv[]) { switch(handleargs(argc,argv)){ case INDENT: doIdent(argv[1]); fclose(fop); break; default: help(); } }
读最后一行肿么不行D。。。。..
还有替换你输入的文件后缀...
filename[strlen(filename)-1]='c';
filename[strlen(filename)-2]='n';
filename[strlen(filename)-3]='e';
之前因为是txt转enc可以这样写。。。
但现在我要XX.c转XX.bak 然后写进XX.tmp···把XX.tmp替换成a.c 这个就弄不到了。。。.