2012-09-08 52 views
0
#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); 
} 

程序,沒有錯誤編譯,當我傳遞參數在scanf甚至有沒有涉及到打開該文件的錯誤時似乎thrown.i不能複製任何媒體文件就像.avi格式一樣,該文件在其目標位置創建,但是隻有0個字節。C程序從一個位置複製媒體文件到另一個

+0

大概'功課。 –

+0

請考慮使用fgets(3)而不是scanf(3)。 scanf(3)存在各種問題。請在這裏看到,http://c-faq.com/stdio/scanfprobs.html – dmp

回答

2

的問題是在你的第二個電話open(2)

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

取而代之的是第二個逗號,你可能是指一個|。正因爲如此逗號O_WRONLY將是第三個參數,該mode和文件將不會有正確的權限。

+0

日Thnx,它的工作! – rtz87

相關問題