簡體   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);
}

程序編譯0錯誤,當我在scanf中傳遞參數時,甚至沒有與打開文件相關的錯誤被拋出。我似乎無法使用此程序復制任何媒體文件,如.avi格式,該文件確實在其目標位置創建但是有0個字節。

問題出在你的第二次open(2)電話中:

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

而不是第二個逗號,你可能意味着| 由於該逗號O_WRONLY將是第三個參數,因此mode和文件將沒有正確的權限。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM