[英]c S_ISDIR and S_ISREG not working
我需要使用C在Linux中制作一個應用程序,我需要輸入一個文件夾路徑並將所有文件和子目錄復制到一個新文件夾中。 問題是,當我嘗試檢查路徑是指向文件夾還是文件時,程序始終采用類似於文件夾的路徑,即使它是文件。 我不知道我在做什么錯。 這是代碼:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <assert.h>
int main()
{
CopyFile("test3","test4");
return (0);
}
void mkdirRecursive(const char *path, mode_t mode) {
char opath[PATH_MAX];
char *p;
size_t len;
strncpy(opath, path, sizeof(opath));
opath[sizeof(opath) - 1] = '\0';
len = strlen(opath);
if (len == 0)
return;
else if (opath[len - 1] == '/')
opath[len - 1] = '\0';
for(p = opath; *p; p++)
if (*p == '/') {
*p = '\0';
if (access(opath, F_OK))
mkdir(opath, mode);
*p = '/';
}
if (access(opath, F_OK))
mkdir(opath, mode);
}
void CopyFile(char* NumeFisOld, char* NumeFisNew)
{
DIR *dp;
struct dirent *dirp;
struct stat info;
char OldDirPath[255];
char NewDirPath[255];
stat(NumeFisOld,&info);
int status = stat (NumeFisOld, &info);
if (status != 0)
{
fprintf (stderr, "Error, errno = %d\n", errno);
perror("stat error: ");
return;
}
fprintf (stderr, "Error, errno = %d\n", errno);
perror("stat error: ");
if(S_ISREG(info.st_mode))
{
printf("File: File location: %s \n",NumeFisOld);
printf("File: New file location: %s \n",NumeFisNew);
char buffer[65];
int fhRead;
int fhWrite;
unsigned int nbytes=65;
int bytesread;
int byteswritten;
if((fhRead=open(NumeFisOld, O_RDONLY)) ==-1)
{
perror("Eroare la deschiderea fisierului");
exit(1);
}
if((bytesread=read(fhRead,buffer,nbytes))<=0)
perror("Probleme la citirea fisierului");
else
printf("Citeste %u bytes din fisier\n", bytesread);
close(fhRead);
if((fhWrite=open(NumeFisNew,O_WRONLY | O_CREAT,S_IREAD | S_IWRITE)) !=-1)
{
if((byteswritten=write(fhWrite, buffer, sizeof(buffer))) == -1)
perror("Eroare la scriere");
else
printf("A scris %u bytes in fisier\n",byteswritten);
close(fhWrite);
}
}
else if(S_ISDIR(info.st_mode))
{
printf("Folder: File location: %s \n",NumeFisOld);
printf("Folder: New file location: %s \n",NumeFisNew);
dp=opendir(NumeFisOld);
while((dirp=readdir(dp)) != NULL)
{
strcpy(OldDirPath, NumeFisOld);
strcat(OldDirPath,"/");
strcat(OldDirPath,dirp->d_name);
strcpy(NewDirPath, NumeFisNew);
strcat(NewDirPath, "/");
strcat(NewDirPath,dirp->d_name);
DIR* dir = opendir(NewDirPath);
if(dirp->d_name[0]!='.')
{
if (dir)
{
/* Directonry exists. */
CopyFile (OldDirPath,NewDirPath);
printf("Directorul %s exista deja \n",NewDirPath);
closedir(dir);
}
else if (ENOENT == errno)
{
printf("Directorul %s nu exista \n",NewDirPath);
mkdirRecursive(NewDirPath,0755);
CopyFile (OldDirPath,NewDirPath);
/* Directory does not exist. */
}
else
{
printf("Directorul %s nu s-a putut deschide \n",NewDirPath);
/* opendir() failed for some other reason. */
}
}
}
}
}
我的邏輯是:首先,我檢查路徑是否為目錄,如果是,則在我的新文件夾(NumeFisNew)中創建它,然后輸入它(NumeFisOld),並為其中的每個子目錄或文件重復。 如果路徑是文件,則應輸入else並將文件復制到路徑(NumeFisNew)中。
編輯:好的,這是結果:
Error, errno = 0
stat error: : Success
Folder: File location: test3
Folder: New file location: test4
Error, errno = 0
stat error: : Success
Folder: File location: test3/test5
Folder: New file location: test4/test5
Directorul test4/test5 exista deja
Error, errno = 0
stat error: : Success
Folder: File location: test3/test4
Folder: New file location: test4/test4
Error, errno = 0
stat error: : Success
File: File location: test3/test4/test2
File: New file location: test4/test4/test2
Citeste 42 bytes din fisier
Directorul test4/test4/test2 exista deja
Directorul test4/test4 exista deja
Error, errno = 21
stat error: : Is a directory
File: File location: test3/test1
File: New file location: test4/test1
Citeste 42 bytes din fisier
Directorul test4/test1 exista deja
如果它前面有File,則表示它是從if(S_ISREG)使用的;如果它有Folder,則意味着它是從if(S_ISDIR)使用的。 因此,至少現在它知道test1和test2是文件,但仍將它們復制為文件夾。
您必須切換控制語句:首先查看它是否是文件,然后檢查它是否為目錄
if (S_ISREG (info.st_mode))
{
printf ("It's a file\n");
}
else if (S_ISDIR (info.st_mode))
{
printf ("It's a dir\n");
}
此外,您應該注意統計返回值
int status = stat (NumeFisOld, &info);
if (status != 0)
{
fprintf (stderr, "Error, errno = %d\n", errno);
perror("stat error: ");
return;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.