Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 我将一个文件复制到另一个文件的程序失败_C_File_System Calls - Fatal编程技术网

C 我将一个文件复制到另一个文件的程序失败

C 我将一个文件复制到另一个文件的程序失败,c,file,system-calls,C,File,System Calls,我试图将文件中写入的内容复制到另一个文件(使用系统调用),但我的代码似乎不起作用。我第一次尝试使用printf()缓冲区打印,但它也不起作用。我猜我读错了文件 #define BUF_SIZE 200 int main(int argc, char *argv[]){ int entrada,salida,leidos; char buffer[BUF_SIZE]; entrada = open(argv[1],O_RDONLY); salida = crea

我试图将文件中写入的内容复制到另一个文件(使用系统调用),但我的代码似乎不起作用。我第一次尝试使用
printf()
缓冲区打印,但它也不起作用。我猜我读错了文件

#define BUF_SIZE 200
int main(int argc, char *argv[]){

    int entrada,salida,leidos;
    char buffer[BUF_SIZE];

    entrada = open(argv[1],O_RDONLY);
    salida = creat(argv[2], 0644);

    while( (leidos = read(entrada,buffer,BUF_SIZE)) > 0 ){
        write(salida,buffer,leidos);

    }

    close(salida);
    close(entrada);

    return 0;
}

我的实现出了什么问题?

我认为您缺少输出上适当的打开标志。尝试:

salida = creat(argv[2], O_WRONLY | O_CREAT, 0644);
但是,正如注释所示,您可能会收到由返回值和/或
errno
变量指示的错误,您将忽略该变量

此外,我将避免使用Castellano特定的变量名。无论如何,编写C/C++都需要懂英语,所以在命名时最好坚持这一点;否则-不会说Castellano的人将很难理解您的代码

最后,你为什么要这样做?有很多更好的C++友好的,甚至是C友好的方法来复制一个文件,它也可以移植(你的代码不是)。见:


正如@einpoklum所述,主要问题可能是您打开输出文件的方式(标志和权限)造成的。总的来说,您的代码远远没有实现最小的调试冗余度,我认为一些代码流控件将在检测代码中的真正问题方面帮助您很多:

#include <stdio.h>
#include <stdlib.h>

#ifndef BUF_SIZE
#define BUF_SIZE 200
#endif

int main(int argc, char *argv[])
{
    int entrada = open(argv[1], O_RDONLY);

    if (entrada == -1)
    {
        fprintf(stderr, "Error opening: %s\n", argv[1]);
        exit(-1);
    }

    int openFlags = O_CREAT | O_WRONLY | O_TRUNC;
    mode_t filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    int salida = open(argv[2], openFlags, filePerms);

    if (salida == -1)
    {
        fprintf(stderr, "Error opening: %s\n", argv[2]);
        exit(-1);
    }

    ssize_t numRead;
    char buf[BUF_SIZE];

    while ((numRead = read(entrada, buf, BUF_SIZE)) > 0)
    {
        if (write(salida, buf, numRead) != numRead)
        {
            fprintf(stderr, "Writing error!\n");
            exit(-1);
        }
    }

    if (numRead == -1)
    {
        fprintf(stderr, "Reading error!\n");
        exit(-1);
    }

    if (close(entrada) == -1)
    {
        fprintf(stderr, "Input closing error!\n");
        exit(-1);
    }

    if (close(salida) == -1)
    {
        fprintf(stderr, "Output closing error!\n");
        exit(-1);
    }

    exit(0);
}
#包括
#包括
#ifndef BUF_尺寸
#定义BUF_尺寸200
#恩迪夫
int main(int argc,char*argv[])
{
int entrada=打开(argv[1],仅限Ordu);
如果(entrada==-1)
{
fprintf(stderr,“错误打开:%s\n”,argv[1]);
出口(-1);
}
int openFlags=O|u CREAT | O|u WRONLY | O|u TRUNC;
模式| t filePerms=S|u IRUSR | S|u IRGRP | S|u IRGRP | S|u IROTH | S|IWOTH;
int-salida=open(argv[2],openFlags,filePerms);
如果(salida==-1)
{
fprintf(stderr,“错误打开:%s\n”,argv[2]);
出口(-1);
}
西泽努姆雷德;
字符大小[buf_SIZE];
而((numRead=read(entrada,buf,buf_SIZE))>0)
{
if(写入(salida,buf,numRead)!=numRead)
{
fprintf(标准“写入错误!\n”);
出口(-1);
}
}
如果(numRead==-1)
{
fprintf(标准“读取错误!\n”);
出口(-1);
}
如果(关闭(entrada)=-1)
{
fprintf(标准“输入关闭错误!\n”);
出口(-1);
}
如果(关闭(salida)=-1)
{
fprintf(标准“输出关闭错误!\n”);
出口(-1);
}
出口(0);
}

您使用的许多函数的返回值可以让您深入了解正在发生的情况。使用它们。

您可能应该从查看函数的返回值开始;打开/创建/读取/写入/关闭可能会在代码中以静默方式失败。。。特别是在C++中,等效文件拷贝是一个单一的语句,不需要使用任何缓冲区或while循环。@ Biffen:因为它是一个格式良好的(尽管是BGGY)C++程序。OP告诉我们,他打算把这个写为C++程序,非常感谢你的回答!很抱歉使用Castellano特定的变量,我总是尽量不使用。至于你的问题,;我这样做是因为我目前正在攻读计算机工程学位(我编写代码的主题是:操作系统简介),我们的老师希望我们使用systemcalls来读取、打开和写入文件,而不是函数。@NicolasGranados:如果你觉得我的答案有用,考虑接受和/或使用它(在信息的左边使用控制)。非常感谢你的完整而快速的回答!Stackoverflow社区每天都给我留下深刻印象。我确实知道我需要一些调试,但我感到非常沮丧,因为我的代码无法工作,所以我完全忘记了它!不,我没有,我试过你的答案和einpoklum,但它似乎不起作用,但我的打印了什么东西吗?或者它只是返回了
0
而没有错误?该死的。。。我确定了我的答案。请现在试一试。