single character replace program 文本中单个字符的替换,支持一些特殊的字符。
2007-09-09 15:31
603 查看
文本中单个字符的替换,支持一些特殊的字符。
用法:
sreplace /b file character1 character2
作用:
将file中的character1全部替换成character2。
说明:
开关 /b 表明备份文件,这时可选项。
character1、character2可以为特殊字符,特殊的字符以 ^ 开头,
^n 表示回车换行符;
^t 表示制表符;
^^ 表示^本身;
^* 表示*。
支持 /? 选项。
源程序:
EXE文件:
dev c++ 4.9.9.2 通过编译。
#include <stdio.h>
#include <stdlib.h>
static void usage(void);
int main(int argc, char *argv[])
...{
int i, c[3];
/**//* two swtiches flag */
int backupfile = 0, question = 0;
FILE *sourcefile, *objfile;
char tmpname[256];
char bakname[256];
/**//* peocess the switches '/', stop when the first charecter is not '/' */
i = 1;
while(i < argc)
...{
if(argv[i][0] == '/')
...{
switch(argv[i][1])
...{
case 'b': backupfile = 1; break;
case '?': question = 1; break;
default: break;
}
}
else
break;
i++;
}
/**//* ask usage or the arguments's number is not correct */
if(question == 1)
...{
usage();
getch();
exit(0);
}
else if( argc - i != 3 )
...{
puts("bad arguments, please type /? for help");
getch();
exit(1);
}
/**//* we know, the impotant three arguments's positions as follow:
argc-3 is the position of filename;
argc-2 is the position of charecter which will be replace;
argc-1 is the position of new charecter; */
sourcefile = fopen(argv[argc-3], "r");
if(sourcefile == NULL)
...{
printf("can not open file %s", argv[argc-3]);
getch();
exit(1);
}
/**//* get a file name randomly */
tmpnam(tmpname);
objfile = fopen(tmpname, "w");
/**//* get the replace charecters */
c[1] = argv[argc-2][0];
c[2] = argv[argc-1][0];
/**//* process the speacial charecter */
i = 1;
while(i < 3)
...{
if(c[i] == '^')
...{
switch(argv[argc-3+i][1])
...{
case 'n': c[i] = ' '; break;
case 't': c[i] = ' '; break;
case '^': c[i] = '^'; break;
case '*': c[i] = '*'; break;
default : break;
}
}
i++;
}
/**//* replace the charecter */
while( (c[0] = fgetc(sourcefile)) != EOF )
c[0] == c[1] ? fputc(c[2], objfile):fputc(c[0], objfile);
fclose(sourcefile);
fclose(objfile);
/**//* change the file name */
if(backupfile == 1)
rename(argv[argc-3], strcat(strcpy(bakname, argv[argc-3]), ".bak"));
else
remove(argv[argc-3]);
rename(tmpname, argv[argc-3]);
return 0;
}
static void usage(void)
...{
puts("sreplace /b filename char1 char2 ");
puts("use char2 to replace with char1 in filename.");
puts("/b: tell program to backup the file.");
puts("special characters are supported by char1 and char2,");
puts("and they begin with ^, as follows:");
puts("^n: CR/LF, ^t: table, ^*: *, ^^: ^.");
}
用法:
sreplace /b file character1 character2
作用:
将file中的character1全部替换成character2。
说明:
开关 /b 表明备份文件,这时可选项。
character1、character2可以为特殊字符,特殊的字符以 ^ 开头,
^n 表示回车换行符;
^t 表示制表符;
^^ 表示^本身;
^* 表示*。
支持 /? 选项。
源程序:
EXE文件:
dev c++ 4.9.9.2 通过编译。
#include <stdio.h>
#include <stdlib.h>
static void usage(void);
int main(int argc, char *argv[])
...{
int i, c[3];
/**//* two swtiches flag */
int backupfile = 0, question = 0;
FILE *sourcefile, *objfile;
char tmpname[256];
char bakname[256];
/**//* peocess the switches '/', stop when the first charecter is not '/' */
i = 1;
while(i < argc)
...{
if(argv[i][0] == '/')
...{
switch(argv[i][1])
...{
case 'b': backupfile = 1; break;
case '?': question = 1; break;
default: break;
}
}
else
break;
i++;
}
/**//* ask usage or the arguments's number is not correct */
if(question == 1)
...{
usage();
getch();
exit(0);
}
else if( argc - i != 3 )
...{
puts("bad arguments, please type /? for help");
getch();
exit(1);
}
/**//* we know, the impotant three arguments's positions as follow:
argc-3 is the position of filename;
argc-2 is the position of charecter which will be replace;
argc-1 is the position of new charecter; */
sourcefile = fopen(argv[argc-3], "r");
if(sourcefile == NULL)
...{
printf("can not open file %s", argv[argc-3]);
getch();
exit(1);
}
/**//* get a file name randomly */
tmpnam(tmpname);
objfile = fopen(tmpname, "w");
/**//* get the replace charecters */
c[1] = argv[argc-2][0];
c[2] = argv[argc-1][0];
/**//* process the speacial charecter */
i = 1;
while(i < 3)
...{
if(c[i] == '^')
...{
switch(argv[argc-3+i][1])
...{
case 'n': c[i] = ' '; break;
case 't': c[i] = ' '; break;
case '^': c[i] = '^'; break;
case '*': c[i] = '*'; break;
default : break;
}
}
i++;
}
/**//* replace the charecter */
while( (c[0] = fgetc(sourcefile)) != EOF )
c[0] == c[1] ? fputc(c[2], objfile):fputc(c[0], objfile);
fclose(sourcefile);
fclose(objfile);
/**//* change the file name */
if(backupfile == 1)
rename(argv[argc-3], strcat(strcpy(bakname, argv[argc-3]), ".bak"));
else
remove(argv[argc-3]);
rename(tmpname, argv[argc-3]);
return 0;
}
static void usage(void)
...{
puts("sreplace /b filename char1 char2 ");
puts("use char2 to replace with char1 in filename.");
puts("/b: tell program to backup the file.");
puts("special characters are supported by char1 and char2,");
puts("and they begin with ^, as follows:");
puts("^n: CR/LF, ^t: table, ^*: *, ^^: ^.");
}
相关文章推荐
- JAVA中REPLACE和REPLACEALL的区别 以及一些特殊字符替换
- 替换文本中的特殊字符
- SQL中的REPLACE替换字符函数的用法(使之支持textntext)
- java 过滤文本特殊字符 以及单斜杠替换成双斜杠
- php自定义函数替换超长文本中的特殊字符
- php替换超长文本中的特殊字符的函数代码
- php str_replace替换特殊字符
- php替换超长文本中的特殊字符的函数代码
- python 替换数据库中文本中特殊字符
- hive中 regexp_replace的用法,替换特殊字符问题
- js替换文本特殊字符
- java中替换一些特殊字符的办法
- python替换UTF-8编码文本中任意特殊字符,包括中文符号
- Android之XML 特殊字符 警告 Replace “…” with ellipsis character (…, &&;#8230;)
- str_replace() 函数使用一个字符串替换字符串中的另一些字符
- SQL中的REPLACE替换字符函数的用法(使之支持text/ntext)
- SQL中的REPLACE替换字符函数的用法(使之支持text/ntext) (2
- python替换UTF-8编码文本中任意特殊字符,包括中文符号问题:大量文本,将其中的特殊字符用空
- JAVA中替换一些特殊字符的方法
- javascript使用replace替换字符串中所有字符