C 将多个文件夹中的图像复制到一个文件夹中

C 将多个文件夹中的图像复制到一个文件夹中,c,posix,dirent.h,C,Posix,Dirent.h,我有一个文件夹,其中有多个文件夹,每个文件夹包含许多照片,例如,我有一个文件夹2018和文件夹2018包含11更多文件夹,所有这11个文件夹都包含一些图像,所有这些都是相同格式的。jpg。我想将不同文件夹中的所有图像复制到一个文件夹中 我尝试了以下代码: #include<stdio.h> #include<stdio.h> #include<dirent.h> #include<errno.h> #include<sys/types.h&g

我有一个文件夹,其中有多个文件夹,每个文件夹包含许多照片,例如,我有一个文件夹
2018
和文件夹
2018
包含
11
更多文件夹,所有这11个文件夹都包含一些图像,所有这些都是相同格式的。jpg。我想将不同文件夹中的所有图像复制到一个文件夹中

我尝试了以下代码:

#include<stdio.h>
#include<stdio.h>
#include<dirent.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/stat.h>
#define MAX 1024

int main()
{
    char    arSrcPath[]    = "/home/mpe4/Src";    /*Source directory path*/
    char    arDestPath[]    = "/home/mpe4/Dest";    /*dest directory path*/
    struct    dirent* spnDirPtr;    /* struct dirent to store all files*/

    DIR* pnOpenDir = NULL;    /*DIR Pointer to open Dir*/
    DIR* pnReadDir = NULL;    /*DIR POinter to read directory*/

    pnOpenDir = opendir(arSrcPath); 

    if(!pnOpenDir)
    printf("\n ERROR! Directory can not be open");

    else
    {    
    int nErrNo = 0;
    while(spnDirPtr = readdir(pnOpenDir))
    {
        if(nErrNo == 0)
        nErrNo = errno;
        printf("\n Now writing %s file...",spnDirPtr->d_name);

        printf("\n dest file name = %s/%s\n", arDestPath, spnDirPtr->d_name);

        struct stat st_buf;
        stat(spnDirPtr->d_name, &st_buf);
        if (S_ISDIR (st_buf.st_mode))
        {
            continue;
        }
        else if (S_ISREG (st_buf.st_mode))
        {
            FILE* pnReadFile = fopen(spnDirPtr->d_name,"r");

            if(pnReadFile)
            {
                printf("\n Now reading %s file...",spnDirPtr->d_name);

                char strDestFileName[MAX] = {0};
                sprintf(strDestFileName, "%s/%s", arDestPath, spnDirPtr->d_name);
                printf("\n dest file name = %s\n", strDestFileName);

                FILE* pnWriteFile  = fopen(strDestFileName, "w");    /*File Pointer to write in file*/
                if(pnWriteFile)
                {
                    char buffer[MAX] = {0};    /*Buffer to store files content*/

                        while(fgets(buffer, MAX, pnReadFile))
                        {
                            fputs(buffer, pnWriteFile);
                        }
                    fclose(pnWriteFile);
                }
            else
            {
                printf("\n Unable to open file %s", strDestFileName);
            }
            fclose(pnReadFile);
    }
    else
    {
        printf ("\nERROR! File Could not be open for reading");
    }
    }
    }
    if(nErrNo != errno)
        printf ("\nERROR Occurred!\n");
    else
        printf ("\nProcess Completed\n");

    }
    closedir(pnOpenDir);
    return 0;
}  
#包括
#包括
#包括
#包括
#包括
#包括
#定义最大1024
int main()
{
char arSrcPath[]=“/home/mpe4/Src”;/*源目录路径*/
char-arDestPath[]=“/home/mpe4/Dest”;/*Dest目录路径*/
struct dirent*spnDirPtr;/*struct dirent存储所有文件*/
DIR*pnOpenDir=NULL;/*DIR指向打开目录的指针*/
DIR*pnReadDir=NULL;/*DIR指向读取目录的指针*/
pnOpenDir=opendir(arSrcPath);
如果(!pnOpenDir)
printf(“\n错误!无法打开目录”);
其他的
{    
int-nErrNo=0;
while(spnDirPtr=readdir(pnOpenDir))
{
如果(nErrNo==0)
nErrNo=errno;
printf(“\n正在写入%s文件…”,spnDirPtr->d_名称);
printf(“\n目标文件名=%s/%s\n”,arDestPath,spnDirPtr->d_名称);
结构统计数据;
统计(spnDirPtr->d_名称和st_buf);
中频(S_ISDIR(st_buf.st_模式))
{
继续;
}
else if(S_ISREG(圣布夫圣布夫模式))
{
FILE*pnReadFile=fopen(spndirprtr->d_name,“r”);
如果(pnReadFile)
{
printf(“\n正在读取%s文件…”,spnDirPtr->d_名称);
char strDestFileName[MAX]={0};
sprintf(strDestFileName,“%s/%s”,arDestPath,spndirpt->d_name);
printf(“\n dest文件名=%s\n”,strDestFileName);
FILE*pnWriteFile=fopen(strDestFileName,“w”);/*要写入文件的文件指针*/
if(pnWriteFile)
{
char buffer[MAX]={0};/*用于存储文件内容的缓冲区*/
while(fgets(缓冲区、最大值、pnReadFile))
{
fputs(缓冲区、pnWriteFile);
}
fclose(pnWriteFile);
}
其他的
{
printf(“\n无法打开文件%s”,strDestFileName);
}
fclose(pnReadFile);
}
其他的
{
printf(“\n错误!无法打开文件进行读取”);
}
}
}
如果(nErrNo!=errno)
printf(“\n发生错误!\n”);
其他的
printf(“\n进程已完成\n”);
}
closedir(pnOpenDir);
返回0;
}  

但此代码只是复制文件,而不是从文件中复制数据。

fgets
用于文本文件,而不是二进制文件。JPEG图像是一个二进制文件。另外,是否有特别的理由为可以用shell脚本一行完成的事情编写C程序?您的标题也不正确。您正在将多个文件夹/目录中的图像复制到一个文件夹/目录中。@AnttiHaapala您能帮我写这行吗?