文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>写的一个linux下修改文件类型的小程序。

写的一个linux下修改文件类型的小程序。

时间:2007-06-15  来源:Wing.J

最近碰到一件事,需要把修改.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  :            [email protected]
  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 <stdio.h>
#include <dirent.h>
#include <sys/types.h>

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
相关阅读 更多 +
排行榜 更多 +
爱是小事最新版

爱是小事最新版

休闲益智 下载
悬案2刹那惊颤游戏

悬案2刹那惊颤游戏

冒险解谜 下载
几何飞行内购修改版

几何飞行内购修改版

飞行射击 下载