【问题标题】:making a new directory for my copied file in C在 C 中为我复制的文件创建一个新目录
【发布时间】:2016-08-29 13:50:53
【问题描述】:

我想有一个成功的复制文件程序,在那里我可以得到结果: ./a.out 1.c examples3/16.c 是成功的,我可以成功地为我复制的文件创建一个新目录。你怎么能这样做,因为我尝试这样做时出错。我可以在一个地方成功复制 1.c 文件,并且可以将文件复制到现有目录中,但无法成功将文件复制到新建目录中。你怎么能做到这一点并解决这个问题?

代码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

#define BUFFERSIZE      4096
#define COPYMODE        0644
int file_exist (char *filename)
{
 struct stat   buffer;   
 return (stat (filename, &buffer) == 0);
}

int main(int ac, char *av[])
{
int ch;  //ch - character no.                               
struct stat sb;
char directory[120];
FILE *source, *target;
/* from http://stackoverflow.com/questions/30215462/how-to-get-the-source-files-the-file-which-i-want-to-copy-and-the-copied-file/30217023#30217023 */
if (ac <= 2) {
    printf("Enter source and destination file names\n");
    exit(EXIT_FAILURE);
}

if ( strcmp(av[1], av[2]) ==0 )
{
    printf("the files are the same\n");
    exit(1);
}

if (file_exist (av[2]))
 {
  printf ("the destination file exists\n");
  exit(1);
 }
 //printf("which directory do you want to send your destination file?");
 //scanf("%s", directory);

 char serv_name[1000];
 mkdir("newdir/", S_IRWXU | S_IRWXG | S_IRWXO);
 snprintf(serv_name, sizeof(serv_name), "newdir/%s", av[2]);
 FILE* f = fopen(serv_name, "w");
 if (f < 0) {
    perror("CLIENT:\n");
    exit(1);
 }  

 source = fopen(av[1], "r");//getting and opening source file
 if( source == NULL ) {
    printf("Press any key to exit...\n");
    exit(EXIT_FAILURE);
 }

 target = fopen(av[2], "w");//getting and opening destination file

 if( target == NULL ) {
    fclose(source);
    printf("Press any key to exit...\n");
    exit(EXIT_FAILURE);
 }

 while( ( ch = fgetc(source) ) != EOF )
    fputc(ch, target);

 fclose(source);
 fclose(target);
 printf("File copied successfully.\n");
 /* from http://stackoverflow.com/questions/30215462/how-to-get-the-source-files-the-file-which-i-want-to-copy-and-the-copied-file/30217023#30217023 */
 if (stat(av[1], &sb) == -1) {
    perror("stat");
    exit(1); //exit(EXIT_SUCCESS);
  }
  else
  if (chmod(av[2], sb.st_mode & 07777))//http://stackoverflow.com/questions/18032574/how-can-i-copy-permissions-from-a-file-that-already-exists
  {
    perror("chmod");
  }

  printf("Source File: %s, Inode number: %d, Mode: 0x%04X\n", av[1],  (unsigned)sb.st_ino, (unsigned)sb.st_mode);

  if (stat(av[2], &sb) == -1) {
    perror("stat");
    exit(1);
  }

  char *str;
  str = (char *) malloc(15);
  strcpy(str, av[2]);

  if (stat(av[2], &sb) == -1)    {//http://stackoverflow.com/questions/7430248/creating-a-new-directory-in-c
    mkdir(av[2], 0700);
   }
  printf("Destination File: %s, inode number: %d, Address = %u, Mode: 0x%04X\n", av[2], (unsigned)sb.st_ino, str, (unsigned)sb.st_mode);

   free(str);
   return 0;
   }

【问题讨论】:

  • 你考虑过使用调试器吗?或者进行基本的 printf 调试?
  • 那么检查mkdir 的返回值呢?如果由于某种原因无法创建目录会怎样?
  • 打开av[2]和newdir/av[2]有什么意义?你的程序在终端上打印什么?
  • 我正在尝试使用该目录中的复制文件创建一个目录。终端打印出:“Press any key to exit...”

标签: c linux directory copy


【解决方案1】:

我可以成功地为我复制的文件创建一个新目录。 [...] 但未能成功将文件复制到新目录或其他目录中

问题可能来自目录创建:

如果你调用你的程序:./a.out foo bar

snprintf(serv_name, sizeof(serv_name), "newdir/%s", av[2]);
FILE* f = fopen(serv_name, "w");

你的程序会尝试打开newdir/bar:OK

如果你调用你的程序:./a.out foo /path/to/bar

snprintf(serv_name, sizeof(serv_name), "newdir/%s", av[2]);
FILE* f = fopen(serv_name, "w");

你的程序会尝试打开newdir//path/to/bar:那肯定会失败。

所以你的问题来自:

snprintf(serv_name, sizeof(serv_name), "newdir/%s", av[2]);
FILE* f = fopen(serv_name, "w");
if (f < 0) {
    perror("CLIENT:\n");
    exit(1);
} 

如果树目录不正确,您将无法打开f。

【讨论】:

  • 感谢您告诉我这个问题。我现在修好了。
猜你喜欢
  • 1970-01-01
  • 2011-11-17
  • 1970-01-01
  • 2021-11-27
  • 1970-01-01
  • 1970-01-01
  • 2021-02-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多