免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 807 | 回复: 0
打印 上一主题 下一主题

写的一个linux下修改文件类型的小程序。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-06-15 13:08 |只看该作者 |倒序浏览
最近碰到一件事,需要把修改.cpp文件修改成.cc文件~~所以就写了这个程序(或许对某些人有用贴上来):
/*******************************************************************
  This is a program that can change the type of files in  Linux.
  Author :            LiuJie        
  Address:    University of Science and Technology Beijing
  Email  :            lj_533@163.com
  usage  :       changetype [-a][.oldtype] [.newtype]
                      if you chose the -a option, you could change the
                        type of all files to .newtype.
  Date   :            2006-6-15
  File     :            changetype.c
********************************************************************/
#include
#include
#include
int main( int argc, char *argv[])
{
    char strChan[100];
    char strChaned[100];
    if( argc >1 )
    {
        sscanf( argv[1], "%s", strChaned );
        sscanf( argv[2], "%s", strChan );
        if( strChaned[0] == '-' )
        {
            if( strChaned[1] == 'a' || strChaned[1] == 'A' )
            {
                strcpy(strChaned, ".");
            }
            else
            {
                fprintf( stderr, "usage: changetype [-a][.oldtype] [.newtype]\n");
                exit(1);
   
            }
        }
        else if( strChaned[0] != '.' || strChan[0] != '.' )
        {
            fprintf( stderr, "usage: changetype [-a][.oldtype] [.newtype]\n");
            exit(1);
        }
    }
    else
    {
        fprintf( stderr, "usage: changetype [-a][.oldtype] [.newtype]\n");
        exit(1);
   
    }
   
    DIR * dir;
     struct dirent* ptr=NULL;
    dir = opendir(".");
    char newName[1024];
     while ((ptr = readdir(dir)) != NULL)
    {
        if (ptr->d_name[0] != '.')
        {
            if(strstr(ptr->d_name,strChaned))
            {
            /*    printf( "  %s\n", ptr->d_name);*/
                strcpy( newName, ptr->d_name );
   
                strtok(newName,".");
                strcat(newName,strChan);
            /*    printf("  %s\n", newName);
                printf("  %s\n", ptr->d_name);*/
                if( rename(ptr->d_name, newName) == 0)
                {
                    printf("Good! %s files change to %s files\n",ptr->d_name, newName );
                }
                else
                {
                    perror("ERROR!\n");
                }
            }
        }
    }
    return 0;
}
[root@localhost  root]#gcc - o changetype changetype.c
[root@localhost  root]#./changetype .cpp .cc
Good! BatterySimple.cpp files change to BatterySimple.cc files
Good! RadioSimple.cpp files change to RadioSimple.cc files
Good! CPUSimple.cpp files change to CPUSimple.cc files
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/38577/showart_321859.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP