随笔 - 74, 文章 - 0, 评论 - 26, 引用 - 0
数据加载中……

遍历文件目录--生成define

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

static void strtoupper(char *p)
{
 int i;
 int j=strlen(p);
 for(i=0;i<j;i++)
  {
 *p=toupper(*p);
 p++;
 }
};
int main( void )
{
 
 
 int fd;
 DIR * dir;
 struct dirent * ent;
 
 char *p1;
 char *p2;
 char temp[255];
 char lower[50];
 memset(temp,0,sizeof(temp));
 memset(lower,0,sizeof(lower));
 
 
 if(!(dir=opendir("/opt/sip_ui/res/."))){ //目录
 perror("opendir");
 return;
 }
 errno=0;
 

 if((fd=open("hw",O_TRUNC|O_CREAT|O_WRONLY,0644))<0){
 perror("open");
 exit(1); 
 }
 
 while((ent=readdir(dir))){
  
 if(strstr(ent->d_name,".bmp")){ //后缀名
 p1=strtok(ent->d_name,".");
 p2=strtok(NULL,".");
 memcpy(lower,p1,strlen(p1));
 lower[strlen(p1)]='\0';
 strtoupper(p1);
 strtoupper(p2);
 
  strcat(p1,"_PIC");
 //strcat(p1,p2);
 
 sprintf(temp,"#define %s \"res\" PATH_SEP \"%s.\" RESFILE_EXT\n",p1,lower);
 
      
     

 if(write(fd,temp,strlen(temp)) < 0){
 perror("write");
 exit(1);
 }
 }
 errno=0;
 }
 if(errno)
 {
 perror("readdir");
 return ;
 }
 
 close(fd);
 closedir(dir);
 return 1;
}

posted on 2006-06-26 19:18 井泉 阅读(370) 评论(0)  编辑 收藏 引用 所属分类: c code


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理