single character replace program 文本中单个字符的替换,支持一些特殊的字符。

文本中单个字符的替换,支持一些特殊的字符。

用法:

sreplace  /b   file  character1 character2

作用:

将file中的character1全部替换成character2。

说明:

开关 /b 表明备份文件,这时可选项。

character1、character2可以为特殊字符,特殊的字符以 ^ 开头,

  1. ^n 表示回车换行符;
  2. ^t  表示制表符;
  3. ^^ 表示^本身;
  4. ^* 表示*。

支持 /? 选项。

源程序:

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 = 1break;
                
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, ^*: *, ^^: ^.");
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值