linux环境下,根据记录文件,群更改文件名

/*Copyright (C) 2012, schux*/

/*linux环境下:根据已有记录文件,群更改文件名
**记录文件形式如:fdsa^1^我的祖国^2^我的中国心^,标识字符为'^'
**把"1.mp3"更改为"我的祖国.mp3",把"2.mp3"更改为"我的中国心.mp3"
*/

#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*打开当前目录,传入文件类型及类型名长度*/
void call( struct dirent *ptr, char *npt, size_t n );
/*在记录文件中取得新的目录所对应要更换的新的文件名,比如目录中是"1.mp3"更换为"我的祖国.mp3"*/
char *newFileName( FILE *fp );
/*取得特定字符object在文件fp中所在的位置*/
long int charPos( FILE *fp, char object );

int main()
{
  DIR *dir;
  struct dirent *ptr;
  char *nptr;
  size_t n;
 
  dir = opendir( "./" );/*打开当前目录*/
  while( (ptr = readdir(dir)) != NULL )/*当文件名ptr不为空时*/
    {
      if ( (nptr = strrchr(ptr->d_name, '.')) != NULL )/*当找到字符'.'最后出现的位置时*/
        {
          n = strlen( nptr );
          if ( n >= 2 )/*文件有后缀时,比如:.c, .mp3*/
            {
              if ( strlen(ptr->d_name) - n >= 1 )/*文件名不为空时*/
                {
                  printf( "oldFileName:%s\n", ptr->d_name );
                  call( ptr, nptr, n );
                }
              
            }/*end if*/
        }
    }/*end while*/
 
  closedir( dir );

  return 0;
}

void call( struct dirent *ptr, char *nptr, size_t n )
{
  char pstr[256];/*目录中的文件名,不包括后缀".mp3"*/
  char source[256];/*记录文件中的文件名*/
  char new[256];/*新的文件名,包括文件后缀*/
  long int flag_pos = 0;
  size_t p;
  FILE *fptr;
 
  /*取得目录文件名*/
  p = strlen( ptr->d_name ) - n;
  strncpy( pstr, ptr->d_name, p );
  pstr[p] = '\0';
 
  if ( (fptr = fopen("local.txt", "rb")) != NULL )
    {
      rewind( fptr );
      while ( !feof(fptr) )
        {    
          if ( fgetc(fptr) == '^' )/*在记录文件中找到标示符'^'*/
            {
              if ( fread(source, sizeof(char), p, fptr) )
                {
                  source[p] = '\0';                                                
                  if ( !strcmp( pstr, source ) )/*目录中的文件中与记录文件中的文件名比较相同时*/
                    {                             
                      strcpy( new, newFileName(fptr) );
                      strcat( new, nptr );
                      rename( ptr->d_name, new );
                      printf( "newFileName:%s\n", new );
                      break;
                    }
                  else
                    {
                      fseek( fptr, flag_pos++, SEEK_SET );/*比较不相同时文件指针往前移一位*/
                    }
                }/*end if*/
            }
         
        }/*end while*/
    }/*end if*/
    
  fclose( fptr );
}

char *newFileName( FILE *fp )
{
  long int cur_pos;
  long int obj_pos;
  static char temp[256] = "error";
 
  fseek( fp, 1, SEEK_CUR );/*跳过标示符'^'开始查找下一个标示符'^'*/
  cur_pos = ftell( fp );

  obj_pos = charPos( fp, '^');
    
  if ( obj_pos != -2 )/*当检查标示符指针读取正确时*/
    {
      fread( temp, sizeof(char), obj_pos - cur_pos, fp );/*读取记录文件中新的文件名*/
    }
 
  temp[obj_pos - cur_pos] = '\0';
 
  return temp;
}

long int charPos( FILE *fp, char object )
{
  long int pos = -1;
  long int cur_pos;

  cur_pos = ftell( fp );
  while ( !feof(fp) )
    {
      
      if ( fgetc(fp) == object )
        {
          pos = ftell( fp );
          break;
        }
    }  
 
  fseek( fp, cur_pos, SEEK_SET );/*置指针回到原来开始读取的位置*/
 
  return ( pos - 1 );
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值