gpt4 book ai didi

c - 在 C 中为我复制的文件创建一个新目录

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:07:19 25 4
gpt4 key购买 nike

我想有一个成功的复制文件程序,在那里我可以得到结果: ./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;
}

最佳答案

I can successfully make a new directory for my copied file. [...] but not successful in copying the file into a new directory or different

问题可能出在目录创建上:

如果你调用你的程序:./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

关于c - 在 C 中为我复制的文件创建一个新目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37022401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com