2012-09-08 55 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

相关问题