美文网首页
c语言创建一个文件夹以及修改图片的名字

c语言创建一个文件夹以及修改图片的名字

作者: 陈果123 | 来源:发表于2021-08-18 22:15 被阅读0次
    
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <mmsystem.h>
    #pragma comment(lib,"winmm.lib")
    #include "conio.h"
    //#include "dir.h"
    #include "shlwapi.h"
    #pragma comment(lib,"shlwapi.lib")
    #include <io.h> //finddata
    #include <string.h>//字符串操作
    #define MAX 128
    typedef struct music {
        int num;
        char* name;
        struct music* next;
    }musicNode,*linkMusic;
    linkMusic ini(char* name) {
        linkMusic p = (linkMusic)malloc(sizeof(musicNode));
        p->name = name;
        printf("%p,%p\n", name, p->name);
        printf("%s,%s\n", name, p->name);
        printf("%x,%x\n", *name, *p->name);
        return p;
    }
    int main()
    {
        //char str[] = "hellow world";
        //char* p = str;
        //*p ='m';
        //p++;
        //*p = 'i';
        //printf("%s\n", str);
        ////str[] = "ni";
        //p = "mike jiang";
        //printf("%s\n", p);
    
        //char* q = "test";
        //printf("%s\n", q);
        //char s1[] = "你好";
        //char s2[] = "你好a ";
        //linkMusic p = ini("你好");
        ////linkMusic p = ini(s1);
        //printf("%s\n", p->name);
        //linkMusic p1 = ini("你好啊");
        ////linkMusic p1 = ini(s2);
        //printf("%s\n", p1->name);
        /*
        ********在某一个文件夹下面创建新的文件夹**************
        system("mkdir C:\\music\\我的123");
        char path[200]="mkdir C:\\music\\";
        char final[200];
        char tem[20] = "0";
        for (int i = 1; i < 32; i++)
        {
            if (i<10)
            {
                tem[1] = char(i+'0');
                sprintf(final, "%s第%s组答辩照片", path, tem);
            }
            else
            {
                sprintf(final, "%s第%d组答辩照片", path, i);
            }
            
            system(final);
        }*/
    
        char test[100] = "21620102144801.JPG";
        int num = strlen(test);//获取字符串长度
        int i = 0;
        for (int j = 0; j < num; j++)
        {
            if (test[j] != '.') {
                i++;
            }
            else
            {
                break;
            }
    
        }
        printf("长度是:%d\n", i);
        //文件地址(修改这里就可以了)
        char ADDR[100] = "C:\\Users\\cg123\\Desktop\\0818四川普通文理平行1征\\0818四川普通文理平行1征\\照片\\";
        //修改成的文件类型
        char type[10] = ".jpg";
        //char SreachAddr[200] = "C:\\Users\\cg123\\Desktop\\0818省外(除云南)\\0818省外(除云南)\\照片\\*.*";
        char searchAddr[200] = "";
        sprintf(searchAddr,"%s%s", ADDR, "*.*");
        char newP[100] = "";
        char oldP[100] = "";
        char temp[100] = "";
        intptr_t Handle;//卧槽,居然要用这个类型才会不报错
        struct _finddata_t FileInfo;
        Handle = _findfirst(searchAddr, &FileInfo);
        if (-1 == Handle)
            return -1;
        //index(FileInfo.name, '.');
        //printf("%s,%d\n", FileInfo.name, strchr(FileInfo.name, '.'));
        //printf("sss:%d\n", FileInfo.name, strchr(FileInfo.name, '.'));
        
        sprintf(oldP, "%s%s", ADDR, FileInfo.name);
        strncpy(temp, FileInfo.name, 14);
        strcat(temp, type);
        sprintf(newP, "%s%s", ADDR, temp);
        rename(oldP, newP);//用系统函数修改文件的名字
        while (!_findnext(Handle, &FileInfo))
        {
            memset(newP, 0, strlen(newP));//清空字符数组的所有内容
            memset(temp, 0, strlen(temp));
            memset(oldP, 0, strlen(oldP));
            printf("%s\n", FileInfo.name);
            sprintf(oldP, "%s%s", ADDR, FileInfo.name);
            strncpy(temp, FileInfo.name, 14);//将字符串前14个字符进行复制
            strcat(temp, type);//拼接后缀
            
            sprintf(newP, "%s%s", ADDR, temp);
            printf("旧:%s\n", oldP);
            printf("新:%s\n", newP);
            rename(oldP, newP);//去旧地址的位置找到文件,然后将名字改为新地址的名字,如果新旧地址不一样,则表现为移动文件的位置
            //printf("%s,%d\n", FileInfo.name, strchr(FileInfo.name, '.'));
            //strncpy(newP, FileInfo.name, 14);
            //printf("前缀:%s\n", newP);
            //rename("c:\\music\\1.mp3", "1ff.mp3");
        }
        _findclose(Handle);
        
        getchar();
        return 0;
    }
    
    

    相关文章

      网友评论

          本文标题:c语言创建一个文件夹以及修改图片的名字

          本文链接:https://www.haomeiwen.com/subject/ivsnbltx.html