“我该如何解决?”;复制:错误的文件描述符";在我的C程序中

“我该如何解决?”;复制:错误的文件描述符";在我的C程序中,c,posix,C,Posix,您好,我正在开发一个程序,该程序使用POSIX函数像cat程序一样打印所有字符,该程序在打印时必须获得多个文件,并将所有文件的字符写入目标文件。 比如, mycat.exe x.txt y.txt z.txt dest.txt 意味着将x.txt、y.txt和z.txt中的所有字符写入运行程序后创建的文件目标dest.txt。 如果任何文件未退出,程序将不会退出,它将打印一份报告,其中 文件不存在 如果出现任何错误,您必须退出程序 当我编译它时,它编译时没有任何错误(我在Windows 10中编

您好,我正在开发一个程序,该程序使用POSIX函数像cat程序一样打印所有字符,该程序在打印时必须获得多个文件,并将所有文件的字符写入目标文件。
比如,
mycat.exe x.txt y.txt z.txt dest.txt

意味着将x.txt、y.txt和z.txt中的所有字符写入运行程序后创建的文件目标dest.txt。 如果任何文件未退出,程序将不会退出,它将打印一份报告,其中
文件不存在
如果出现任何错误,您必须退出程序

当我编译它时,它编译时没有任何错误(我在Windows 10中编译),但当我尝试运行它时,它显示错误

我是这样编译的:

gcc -o mycp.exe mycp.c
我是这样运行它的:

mycp.exe x.txt y.txt z.txt dest.txt
这就是错误:

copy: Bad file descriptor
这是mycp.c中的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>

#define BUFSIZE     1024


void exit_sys(const char* msg)
{
    perror(msg);
    exit(EXIT_FAILURE);
}

void exit_fail(const char* msg)
{
    fprintf(stderr, "%s\n", msg);
    exit(EXIT_FAILURE);
}

void remove_dest_file(int fd, char** argv, int i)
{

    _close(fd);
    unlink(argv[i]);
}

int copy_file(int fdd, int fds)
{
    char buf[BUFSIZE];
    int n;
    while ((n = read(fds, buf, BUFSIZE)) > 0)
    {
        _write(fdd, buf, n);
    }

    if (n < 0)
    {
        return n;
    }
    return 0;
}

int main(int argc, char** argv)
{
    int fds, fdd;
    int n;
    char buf[BUFSIZE];
    int ch;
    int flags;
    int argcm1;
    int i;


    flags = _O_WRONLY | _O_CREAT;

    if (argc < 2)
        exit_fail("usage:mycp.exe file1.exe file2.exe file3.exe ... filen filedest.exe");

    argcm1 = argc - 1;

    if (!access(argv[argcm1], F_OK)) {
        printf("The file %s exists. Do you want to overwrite?[y]\n", argv[argcm1]);
        ch = getchar();
        if (ch == 'y' || ch == 'Y')
            flags |= _O_TRUNC;
        else
            exit(EXIT_SUCCESS);
    }


    if ((fdd = _open(argv[argcm1], flags, _S_IREAD | _S_IWRITE)) < 0)
    {
        exit_sys("open for destination");
    }



    for (i = 0; i < argcm1; ++i)
    {
        if (fds = _open(argv[i], _O_RDONLY) < 0)
        {
            remove_dest_file(fdd, argv, argcm1);
            exit_sys("open");
        }
        if (copy_file(fds, fdd) < 0)
        {
            remove_dest_file(fdd, argv, argcm1);
            exit_sys("copy");
        }
        _close(fds);

    }
    printf("Succes");
    _close(fdd);

    return 0;
}

#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义bufsize1024
无效退出系统(常量字符*消息)
{
佩罗尔(味精);
退出(退出失败);
}
无效退出失败(const char*msg)
{
fprintf(stderr,“%s\n”,msg);
退出(退出失败);
}
作废删除目标文件(int-fd,char**argv,int-i)
{
_关闭(fd);
解除链接(argv[i]);
}
int copy_文件(int fdd,int fds)
{
字符buf[BUFSIZE];
int n;
而((n=读取(fds、buf、BUFSIZE))>0)
{
_写入(fdd、buf、n);
}
if(n<0)
{
返回n;
}
返回0;
}
int main(int argc,字符**argv)
{
int fds,fdd;
int n;
字符buf[BUFSIZE];
int-ch;
国际旗帜;
int-argcm1;
int i;
flags=_O_WRONLY | _O_CREAT;
如果(argc<2)
退出失败(“用法:mycp.exe file1.exe file2.exe file3.exe…filen filedest.exe”);
argcm1=argc-1;
如果(!访问(argv[argcm1],F_确定)){
printf(“文件%s存在。是否覆盖?[y]\n],argv[argcm1]);
ch=getchar();
if(ch='y'| | ch='y')
旗帜|=真理;
其他的
退出(退出成功);
}
如果((fdd=_打开(argv[argcm1],标志,_S_IREAD | u S_IWRITE))<0)
{
退出系统(“目的地开放”);
}
对于(i=0;i
对于(i=0;i,您有
。您知道,
argv[0]
是可执行文件名,而不是“x.txt”?
int-copy\u-file(int-fdd,int-fds)
的参数是这样的,但您用它们反过来调用:
copy\u-file(fds,fdd)
。投票以“打字错误”结束。非常感谢你解决了这个问题,现在我为没有注意到这个愚蠢的错误而感到尴尬:D.你有(I=0;I
。您知道,
argv[0]
是可执行文件名,而不是“x.txt”?
int-copy\u-file(int-fdd,int-fds)
的参数是这样的,但您用它们反过来调用:
copy\u-file(fds,fdd)
。投票以“打字错误”结束。非常感谢你解决了这个问题,现在我为没有注意到那个愚蠢的错误而感到尴尬:D。