我希望有一个成功的复制文件程序,我可以得到结果:./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;
}
答案 0 :(得分:3)
我可以为我复制的文件成功创建一个新目录。 [...]但没有成功将文件复制到新目录或不同的
问题可能来自目录创建:
如果您通过以下方式致电您:[{1}}
./a.out foo bar
您的程序将尝试打开snprintf(serv_name, sizeof(serv_name), "newdir/%s", av[2]);
FILE* f = fopen(serv_name, "w");
:确定
如果您通过以下方式致电您:[{1}}
newdir/bar
您的程序将尝试打开./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
。