- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我需要使用 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;
}
关于c S_ISDIR 和 S_ISREG 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44239293/
我在仅计算目录中的常规文件时遇到了一些问题。 这是我的代码: int show_regular_files(char ** path) { DIR * dp = opendir(*path);
问题 posix 宏 S_ISREG、S_ISDIR 等仅限于 linux 吗?我需要找出答案,因为我正在尝试编译 CURL它正在尝试在 Windows 上使用它们 我可以使用什么包含文件在 Wind
所以我想测试给定的文件是否正常。 #include #include #include #include #include #include int main(int argc, char
所以我想测试给定的文件是否正常。 #include #include #include #include #include #include int main(int argc, char
我在检索文件属性的 C 程序中遇到宏 S_ISREG()。不幸的是,网上没有关于这个宏的任何基本信息。关于它有一些更高级的讨论,但它们超出了我正在寻找的范围。 什么是 S_ISREG(),它有什么作用
我需要使用 C 在 linux 中制作一个应用程序,我需要输入一个文件夹路径并将所有文件和子目录复制到一个新文件夹中。问题是当我尝试检查路径是文件夹还是文件时,程序总是采用文件夹的路径,即使它是文件。
我正在尝试获取路径中的项目列表并确定它是文件还是文件夹。 我正在 Windows 中运行该脚本。 sftp = sftp_connection(host, username, password, IN
为什么我的 S_ISDIR 和 S_ISREG 不能返回正确的目录/文件名? 当我使用我用 3 个其他目录和一个文本文件创建的测试目录运行程序时,只有 Dir 的 printf() 执行。 #incl
这是我编写的一个 C 程序,用于递归导航和输出目录和常规文件。它在我的 Linux 机器上编译和运行良好。但是在 Solaris 上,dit->d_type == 8 检查和其他类似的检查不起作用,因
让我们考虑以下 3 行代码: struct stat buffer; status = lstat(file.c_str(), &buffer); bool Flag = S_ISREG
从中获取的代码可以正常编译。它打印目录中的文件名,前面有一个字母选项:d、f、l 或 o 取决于他们的文件类型(o 为其他)。但是,我在目录 /etc/network 上测试了它,该目录有一个名为 r
我是一名优秀的程序员,十分优秀!