C仅使用系统调用复制文件,无限运行时
因此,我正在用C编写一个简单的程序,但一直停留在复制部分。程序将命令行上的两个文件名作为参数,并使用系统调用将第一个文件名复制到第二个文件名。如果第二个文件存在,它会询问用户是否要覆盖,如果不存在,它会创建该文件。然而,当用户选择覆盖时,我的程序将无限继续 这是我的密码:C仅使用系统调用复制文件,无限运行时,c,linux,C,Linux,因此,我正在用C编写一个简单的程序,但一直停留在复制部分。程序将命令行上的两个文件名作为参数,并使用系统调用将第一个文件名复制到第二个文件名。如果第二个文件存在,它会询问用户是否要覆盖,如果不存在,它会创建该文件。然而,当用户选择覆盖时,我的程序将无限继续 这是我的密码: #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <stdlib.h> #incl
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
int fd1, fd2;
char buffer[1024];
long int n;
char c;
int num;
if (argc != 3) {
printf("%d\n",argc);
printf("Error, you need to give 2 arguments. Such that [File to copy] [File to create].\n");
exit(1);
}
if (access(argv[1], F_OK) < 0) {
printf("File %s either does not exist or cannot be accessed.\n", argv[1]);
exit(1);
} else {
printf("file %s exists\n", argv[1]);
}
if (access(argv[2], F_OK) < 0) {
printf("File %s does not exist, but one will be created.\n", argv[1]);
fd2=open(argv[2],O_CREAT|O_WRONLY|O_TRUNC, 0700);
} else {
printf("file %s exists\n", argv[2]);
printf("Would you like to overwrite %s? (Type 1 for yes or 0 for no)\n", argv[2]);
scanf("%d%c", &num, &c); // use c to capture \n
if (num == 1) {
fd2=open(argv[2],O_CREAT|O_WRONLY|O_TRUNC, 0700);
} else {
if (num == 0) {
printf("Ok, the file will not be copied and the program will now exit.\n");
exit(1);
} else {
printf("I do not recognize this response, program will now be terminated.\n");
}
}
}
printf("step\n");
while ((n1 = read(fd1, buffer, 1024)) > 0) {
printf("step\n");
if(write(fd2, buffer, n1) != n1){
printf("step\n");
perror("Error writing file.");
printf("step\n");
exit(3);
}
printf("stepss\n");
}
close(fd1);
close(fd2);
}
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
int-fd1,fd2;
字符缓冲区[1024];
长整数n;
字符c;
int-num;
如果(argc!=3){
printf(“%d\n”,argc);
printf(“错误,您需要给出2个参数。例如,[要复制的文件][要创建的文件]。\n”);
出口(1);
}
if(访问(argv[1],F_OK)<0){
printf(“文件%s不存在或无法访问。\n”,argv[1]);
出口(1);
}否则{
printf(“文件%s存在\n”,argv[1]);
}
if(访问(argv[2],F_OK)<0){
printf(“文件%s不存在,但将创建一个。\n”,argv[1]);
fd2=打开(argv[2],O|u CREAT | O|u WRONLY | O|u TRUNC,0700);
}否则{
printf(“文件%s存在\n”,argv[2]);
printf(“是否要覆盖%s?(键入1表示是,键入0表示否)\n”,argv[2]);
scanf(“%d%c”、&num、&c);//使用c捕获\n
如果(num==1){
fd2=打开(argv[2],O|u CREAT | O|u WRONLY | O|u TRUNC,0700);
}否则{
如果(num==0){
printf(“确定,文件不会被复制,程序现在将退出。\n”);
出口(1);
}否则{
printf(“我无法识别此响应,程序现在将被终止。\n”);
}
}
}
printf(“步骤\n”);
而((n1=读取(fd1,缓冲区,1024))>0){
printf(“步骤\n”);
if(写入(fd2,缓冲区,n1)!=n1){
printf(“步骤\n”);
perror(“写入文件时出错”);
printf(“步骤\n”);
出口(3);
}
printf(“步骤”\n);
}
关闭(fd1);
关闭(fd2);
}
printf(“步骤”)用于调试,但它只打印一个。这意味着程序被while循环冻结。我可以使用stat()、open()、read()、write()、close()和access()。任何关于什么是错误或如何做得更好的想法都将不胜感激
有什么问题吗
您的fd1
从未分配,因此read(fd1,…)
返回错误
检查read的返回值和printf(“%m\n”)
将打印详细信息
$ ./a.out a b
file a exists
file b exists
Would you like to overwrite b? (Type 1 for yes or 0 for no)
1
step
Bad file descriptor
有什么问题吗
您的fd1
从未分配,因此read(fd1,…)
返回错误
检查read的返回值和printf(“%m\n”)
将打印详细信息
$ ./a.out a b
file a exists
file b exists
Would you like to overwrite b? (Type 1 for yes or 0 for no)
1
step
Bad file descriptor
哇,太谢谢你了,我真不敢相信我看过了@安索尼塞特比我更能帮助你:)哇,太谢谢你了,我真不敢相信我看过了@AnthonySette比我更能帮助你:)