Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以使用目录流重命名C中的文件?_C_File_Stream_Rename_Dirent.h - Fatal编程技术网

是否可以使用目录流重命名C中的文件?

是否可以使用目录流重命名C中的文件?,c,file,stream,rename,dirent.h,C,File,Stream,Rename,Dirent.h,我是C的新手。 我编写了这个程序,允许我批量重命名同一目录中的文件(大部分显示)。它目前正在使用stdio的Rename函数,同时使用dirent结构查找“旧名称”。但是,这意味着必须将“新名称”和“旧名称”添加到“路径字符串”中,以便Rename可以找到文件。我希望有一种直接使用dirent修改文件名的方法 我尝试将dp->d_名称更改为“新名称”,但这并没有更改文件名 这不是我完整的工作程序,而是我用来测试其他重命名方法的代码 #include <stdio.h> #includ

我是C的新手。 我编写了这个程序,允许我批量重命名同一目录中的文件(大部分显示)。它目前正在使用stdio的Rename函数,同时使用dirent结构查找“旧名称”。但是,这意味着必须将“新名称”和“旧名称”添加到“路径字符串”中,以便Rename可以找到文件。我希望有一种直接使用dirent修改文件名的方法

我尝试将dp->d_名称更改为“新名称”,但这并没有更改文件名

这不是我完整的工作程序,而是我用来测试其他重命名方法的代码

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

int main(){

  DIR *dirp;
  struct dirent *dp;
  char dir[500];
  char pathOne[500] = "Testing.txt";
  int i;


  printf("\nPlease enter the target directory :\n");
  scanf("%[^\n]s",dir);

  dirp = opendir(dir);

  printf(dirp ? "Directory Connection Successful\n\n" : "Directory Connection Failed\n\n");
  printf("%s\n", pathOne);

  while(dp = readdir(dirp)){
    if((dp->d_name[0])!='.'){
      for(i = 0; dp->d_name[i] = pathOne[i]; i++);
      printf("%s\n", dp->d_name);
    }
  }


  return 0;
}

这是我在控制台中得到的,但资源管理器中的文件名没有更改。

struct dirent
表示程序中的目录结构,您将使用
readdir
读取它,修改它的内容,不会影响目录的实际结构

结构
用于保存目录中特定文件的特定信息,因此它与实际文件没有链接

   struct dirent {
       ino_t          d_ino;       /* Inode number */
       off_t          d_off;       /* Not an offset; see below */
       unsigned short d_reclen;    /* Length of this record */
       unsigned char  d_type;      /* Type of file; not supported
                                      by all filesystem types */
       char           d_name[256]; /* Null-terminated filename */
   };

您可以使用
重命名
系统调用重命名实际的
文件

示例:

  while(dp = readdir(dirp)){
    if((dp->d_name[0])!='.'){
      char oldPath[1024], newPath[1024];

      sprintf(oldPath, "%s/%s",dir, dp->d_name);
      sprintf(newPath, "%s/%s",dir, pathOne);
      if (rename(oldPath, newPath) < 0)
        printf("rename error path=%s", oldPath);
    }
  }
while(dp=readdir(dirp)){
如果((dp->d_名称[0])!='。){
字符旧路径[1024],新路径[1024];
sprintf(oldPath,“%s/%s”,dir,dp->d_name);
sprintf(newPath,“%s/%s”,dir,pathOne);
如果(重命名(旧路径、新路径)<0)
printf(“重命名错误路径=%s”,oldPath);
}
}
使用和安装。请注意,C11不知道目录。
  while(dp = readdir(dirp)){
    if((dp->d_name[0])!='.'){
      char oldPath[1024], newPath[1024];

      sprintf(oldPath, "%s/%s",dir, dp->d_name);
      sprintf(newPath, "%s/%s",dir, pathOne);
      if (rename(oldPath, newPath) < 0)
        printf("rename error path=%s", oldPath);
    }
  }