首页 > 技术文章 > 根据txt文件中指定的文件名进行文件分类

ht-beyond 2016-03-05 17:32 原文

根据txt文件中指定的文件名进行文件分类:

// 根据txt文件中指定的文件名进行文件分类

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <windows.h>
#include <io.h>
#include <direct.h>
#include <string.h>

void Remove_End_Char(char a[])
{
    int L = strlen(a);
    for (int i = 0; i <= L; i++)
    if (a[i] == '\n')
    {
        a[i] = '\0';
    }
}

char* Str_join(char *s1, char *s2)
{
    char *result = (char *)malloc(strlen(s1) + strlen(s2) + 1); 
    
    if (result == NULL)
        exit(1);

    strcpy(result, s1);
    strcat(result, s2);

    return result;
}


int main()
{

    char filename[] = "E:\\name.txt";
    FILE *fp;
    char StrLine[260];

    if ((fp = fopen(filename, "r")) == NULL)
    {
        printf("open file error!");
        return -1;
    }


    long Handle;
    struct _finddata_t FileInfo;

    int Same_num = 0;
    char *New_Picture_File;

    // 需要修改
    _chdir("E:\\");

    if ((Handle = _findfirst("*.jpg", &FileInfo)) == -1L)
    {
        printf("没有找到匹配的文件\n");
    }
    else
    {

        while (!feof(fp))
        {
            fgets(StrLine, 260, fp);
             Remove_End_Char(StrLine);

            // 文件名相同
            if (!strcmp(FileInfo.name, StrLine))
            {
                printf("%s\n", StrLine);
                Same_num++;

                // 需要修改
                New_Picture_File = Str_join("E:\\", StrLine);
                rename(StrLine, New_Picture_File);

            }

        }

        // 重置文件指针指向文件头部
        rewind(fp);
        while (_findnext(Handle, &FileInfo) == 0)
        {
            while (!feof(fp))
            {
                fgets(StrLine, 260, fp);
                Remove_End_Char(StrLine);

                // 文件名相同
                if (!strcmp(FileInfo.name, StrLine))
                {
                    printf("%s\n", StrLine);
                    Same_num++;

                    // 需要修改
                    New_Picture_File = Str_join("E:\\", StrLine);
                    rename(StrLine, New_Picture_File);

                    // 这两个都无法使用,参数还没匹配好
                    //system("copy    ");
                    //CopyFile(    );
                    
                }

            }
            rewind(fp);

        }
    
    }
    printf("相同图片的数量是: %d\n", Same_num);


    fclose(fp);
    _findclose(Handle);

    return 0;

}

 

推荐阅读