简体   繁体   English

c程序将媒体文件从一个位置复制到另一个位置

[英]c program to copy media file from one location to another

#include<stdio.h>
#include<stat.h>
#include<fcntl.h>
main()
{
int inhandle,outhandle,bytes;
char source[128],target[128],buffer[512];
printf("enter source file name\n");
scanf("%s",source);

inhandle=open(source,O_RDONLY|O_BINARY);
if(inhandle==-1)
{
    printf("cannot open source file\n");
    exit(0);
}
printf("enter target file name\n");
scanf("%s",target);
outhandle=open(target,O_CREAT|O_BINARY,O_WRONLY,S_IWRITE);
if(outhandle==-1)
{

    printf("cannot open target file\n");
    close(outhandle);
    exit(0);
}
while(1)
{
    bytes=read(inhandle,buffer,512);
    if(bytes>0)
    {
        write(outhandle,buffer,bytes);
    }
    else
    break;
}
close(inhandle);
close(outhandle);
}

program compiles with 0 errors and when i pass the arguments in scanf there even no errors related to opening the file is thrown.i cannot seem to copy any media file like .avi format with this program,the file does gets created in its target location but with 0 bytes. 程序编译0错误,当我在scanf中传递参数时,甚至没有与打开文件相关的错误被抛出。我似乎无法使用此程序复制任何媒体文件,如.avi格式,该文件确实在其目标位置创建但是有0个字节。

The problem is in your second open(2) call: 问题出在你的第二次open(2)电话中:

outhandle=open(target,O_CREAT|O_BINARY,O_WRONLY,S_IWRITE);
                                      ^        ^

Instead of the second comma, you probably meant a | 而不是第二个逗号,你可能意味着| . Because of that comma O_WRONLY will be the third argument, the mode and the file won't have the correct permissions. 由于该逗号O_WRONLY将是第三个参数,因此mode和文件将没有正确的权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM