2016-05-04 56 views
0

我想要一个成功的复制文件程序,我可以得到结果:./a.out 1.c examples3/16.c成功,我可以在那里成功为我的复制文件创建一个新目录。你怎么能做到这一点,因为我试图做到这一点错误。我可以在一个位置成功复制1.c文件,并且可以将文件复制到现有目录中,但不能成功将文件复制到新建的目录中。你怎么能做到这一点并解决这个问题?为我的复制文件创建一个新的目录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; 
    } 
+1

你有没有考虑过使用调试器?或甚至做基本的printf调试? –

+1

以及检查'mkdir'的返回值是什么?如果由于某种原因无法创建目录,会发生什么情况? –

+0

打开'av [2]'和'newdir/av [2]'是什么意思?你的程序在终端上打印什么? – purplepsycho

回答

3

我可以成功地使一个新的目录,我复制的文件。 [...]但在将文件复制到一个新的目录或不同

问题可能来自于目录的创建不成功:

如果你打电话给你编程:./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"); 

您的程序将尝试打开:这肯定会失败。

所以你的问题来自:

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如果树目录是不正确的。

+0

谢谢你告诉我这个问题。我现在修好了。 – user4728257

相关问题