将媒体文件从一个位置复制到另一个位置的c程序 #包括 #包括 #包括 main() { int inhandle,outhandle,字节; 字符源[128],目标[128],缓冲区[512]; printf(“输入源文件名\n”); 扫描频率(“%s”,来源); inhandle=开放(源代码,O|RDONLY | O|U二进制); 如果(inhandle==-1) { printf(“无法打开源文件\n”); 出口(0); } printf(“输入目标文件名\n”); 扫描频率(“%s”,目标); outhandle=open(目标、O|u创建、O|u二进制、O_WRONLY、S|u IWRITE); if(outhandle==-1) { printf(“无法打开目标文件\n”); 关闭(外把手); 出口(0); } 而(1) { 字节=读取(句柄,缓冲区,512); 如果(字节>0) { 写入(输出句柄、缓冲区、字节); } 其他的 打破 } 关闭(手柄); 关闭(外把手); }
程序编译时有0个错误,当我在scanf中传递参数时,甚至没有引发与打开文件相关的错误。我似乎无法使用此程序复制任何类似.avi格式的媒体文件,文件确实是在其目标位置创建的,但有0个字节。问题在于您的第二次将媒体文件从一个位置复制到另一个位置的c程序 #包括 #包括 #包括 main() { int inhandle,outhandle,字节; 字符源[128],目标[128],缓冲区[512]; printf(“输入源文件名\n”); 扫描频率(“%s”,来源); inhandle=开放(源代码,O|RDONLY | O|U二进制); 如果(inhandle==-1) { printf(“无法打开源文件\n”); 出口(0); } printf(“输入目标文件名\n”); 扫描频率(“%s”,目标); outhandle=open(目标、O|u创建、O|u二进制、O_WRONLY、S|u IWRITE); if(outhandle==-1) { printf(“无法打开目标文件\n”); 关闭(外把手); 出口(0); } 而(1) { 字节=读取(句柄,缓冲区,512); 如果(字节>0) { 写入(输出句柄、缓冲区、字节); } 其他的 打破 } 关闭(手柄); 关闭(外把手); },c,file,copy,C,File,Copy,程序编译时有0个错误,当我在scanf中传递参数时,甚至没有引发与打开文件相关的错误。我似乎无法使用此程序复制任何类似.avi格式的媒体文件,文件确实是在其目标位置创建的,但有0个字节。问题在于您的第二次打开(2)调用: #include<stdio.h> #include<stat.h> #include<fcntl.h> main() { int inhandle,outhandle,bytes; char source[128],target[128],
打开(2)
调用:
#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);
}
您可能指的是|
,而不是第二个逗号。因为这个逗号<代码> OYWRUNION/CODE >将是第三个参数,<代码>模式< /代码>,文件将没有正确的权限。请考虑使用FFET(3)代替SCANF(3)。scanf(3)存在各种问题。请看这里,
outhandle=open(target,O_CREAT|O_BINARY,O_WRONLY,S_IWRITE);
^ ^