路径转换问题

whdugh 2014-02-27 05:07:58
在工作中遇到一个问题,就是做路径格式转换,如下:

以"/"开头,可替换的使用以下变量,其他的就是路径名称
例:/wfpic/$YEAR/$MONTH/$DAY 转换后---》/wfpic/2014/02/27
/表示所有的数据存放在FTP跟目录。

以上,想不到好的方法,求大神指点,小弟先谢谢了
...全文
89 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
whdugh 2014-02-27
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
[/code]
谢谢赵老师,我好好看看
帅得不敢出门 2014-02-27
  • 打赏
  • 举报
回复
用shell很容易实现的 zengming@jyx:~/test$ ./replace.sh /wfpic/2014/02/27 zengming@jyx:~/test$ cat ./replace.sh #!/usr/bin/env sh YEAR=`date +%Y` MONTH=`date +%m` DAY=`date +%d` echo /wfpic/$YEAR/$MONTH/$DAY
shiguojie19892 2014-02-27
  • 打赏
  • 举报
回复
不够详细,不明白什么问题。
赵4老师 2014-02-27
  • 打赏
  • 举报
回复
仅供参考
#include <stdio.h>
#include <string.h>
char string[80];
char seps1[3];
char seps2[3];
char *token;
char *zzstrtok (
    char *string,
    const char *control1,//连续出现时视为中间夹空token
    const char *control2 //连续出现时视为中间无空token
    )
{
    unsigned char *str;
    const unsigned char *ctrl1 = control1;
    const unsigned char *ctrl2 = control2;
    unsigned char map1[32],map2[32];
    static char *nextoken;
    static char flag=0;
    unsigned char c;
    int L;

    memset(map1,0,32);
    memset(map2,0,32);
    do {
        map1[*ctrl1 >> 3] |= (1 << (*ctrl1 & 7));
    } while (*ctrl1++);
    do {
        map2[*ctrl2 >> 3] |= (1 << (*ctrl2 & 7));
    } while (*ctrl2++);

    if (string) {
        if (control2[0]) {
            L=strlen(string);
            while (1) {
                c=string[L-1];
                if (map2[c >> 3] & (1 << (c & 7))) {
                    L--;
                    string[L]=0;
                } else break;
            }
        }
        if (control1[0]) {
            L=strlen(string);
            c=string[L-1];
            if (map1[c >> 3] & (1 << (c & 7))) {
                string[L]=control1[0];
                string[L+1]=0;
            }
        }
        str=string;
    }
    else        str=nextoken;

    string=str;
    while (1) {
        if (0==flag) {
            if (!*str) break;
            if (map1[*str >> 3] & (1 << (*str & 7))) {
                *str=0;
                str++;
                break;
            } else if (map2[*str >> 3] & (1 << (*str & 7))) {
                string++;
                str++;
            } else {
                flag=1;
                str++;
            }
        } else if (1==flag) {
            if (!*str) break;
            if (map1[*str >> 3] & (1 << (*str & 7))) {
                *str=0;
                str++;
                flag=0;
                break;
            } else if (map2[*str >> 3] & (1 << (*str & 7))) {
                *str=0;
                str++;
                flag=2;
                break;
            } else str++;
        } else {//2==flag
            if (!*str) return NULL;
            if (map1[*str >> 3] & (1 << (*str & 7))) {
                str++;
                string=str;
                flag=0;
            } else if (map2[*str >> 3] & (1 << (*str & 7))) {
                str++;
                string=str;
            } else {
                string=str;
                str++;
                flag=1;
            }
        }
    }
    nextoken=str;

    if (string==str) return NULL;
    else             return string;
}
void main()
{
   strcpy(string,"A \tstring\t\tof ,,tokens\n\nand some  more tokens, ");
   strcpy(seps1,",\n");strcpy(seps2," \t");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,"1234| LIYI|China | 010 |201110260000|OK");
   strcpy(seps1,"|");strcpy(seps2," ");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,"1234|LIYI||010|201110260000|OK");
   strcpy(seps1,"");strcpy(seps2,"|");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,"1234|LIYI||010|201110260000|OK");
   strcpy(seps1,"|");strcpy(seps2,"");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,"a");
   strcpy(seps1,",");strcpy(seps2,"");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,"a,b");
   strcpy(seps1,",");strcpy(seps2,"");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,"a,,b");
   strcpy(seps1,",");strcpy(seps2,"");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,",a");
   strcpy(seps1,",");strcpy(seps2,"");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,"a,");
   strcpy(seps1,",");strcpy(seps2,"");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,",a,,b");
   strcpy(seps1,",");strcpy(seps2,"");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,",,a,,b,,");
   strcpy(seps1,",");strcpy(seps2,"");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,",");
   strcpy(seps1,",");strcpy(seps2,"");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,",,");
   strcpy(seps1,",");strcpy(seps2,"");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,",,,");
   strcpy(seps1,",");strcpy(seps2," ");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }
}
//
//[A      string          of ,,tokens
//
//and some  more tokens,]
//Tokens:
// <A>, <string>, <of>, <>, <tokens>, <>, <and>, <some>, <more>, <tokens>, <>,
//[1234| LIYI|China | 010 |201110260000|OK]
//Tokens:
// <1234>, <LIYI>, <China>, <010>, <201110260000>, <OK>,
//[1234|LIYI||010|201110260000|OK]
//Tokens:
// <1234>, <LIYI>, <010>, <201110260000>, <OK>,
//[1234|LIYI||010|201110260000|OK]
//Tokens:
// <1234>, <LIYI>, <>, <010>, <201110260000>, <OK>,
//[a]
//Tokens:
// <a>,
//[a,b]
//Tokens:
// <a>, <b>,
//[a,,b]
//Tokens:
// <a>, <>, <b>,
//[,a]
//Tokens:
// <>, <a>,
//[a,]
//Tokens:
// <a>, <>,
//[,a,,b]
//Tokens:
// <>, <a>, <>, <b>,
//[,,a,,b,,]
//Tokens:
// <>, <>, <a>, <>, <b>, <>, <>,
//[,]
//Tokens:
// <>, <>,
//[,,]
//Tokens:
// <>, <>, <>,
//[,,,]
//Tokens:
// <>, <>, <>, <>,
#include <stdio.h>
#include <string.h>
#include <locale.h>
char s[256];
char *p;
int r,n,i;
int main() {
    setlocale(LC_ALL,"chs");
    while (1) {
        printf("请输入一行文字(空行结束),\"%%20\"将替换为\" \",\"你懂得\"将替换为\"XXXXXX\":\n");
        fgets(s,256,stdin);
        if ('\n'==s[0]) break;
        p=s;
        while (1) {
            p=strstr(p,"%20");
            if (p) {
                memmove(p+1,p+3,strlen(p)-3+1);
                p[0]=' ';
            } else break;
        }
        p=s;
        while (1) {
            p=strstr(p,"你懂得");
            if (p) {
                memmove(p+6,p+6,strlen(p)-6+1);
                for (i=0;i<6;i++) p[i]='X';
            } else break;
        }
        printf("%s",s);
    }
    return 0;
}
//请输入一行文字(空行结束),"%20"将替换为" ","你懂得"将替换为"XXXXXX":
//abcdefg%20helloworld%20something.pdf
//abcdefg helloworld something.pdf
//请输入一行文字(空行结束),"%20"将替换为" ","你懂得"将替换为"XXXXXX":
//这是测试文字你懂得,在这个你懂的地方,就得做你懂得的事
//这是测试文字XXXXX,在这个你懂的地方,就得做XXXXX的事
//请输入一行文字(空行结束),"%20"将替换为" ","你懂得"将替换为"XXXXXX":
//

64,692

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧