当前位置: 技术问答>linux和unix
复制文件内容出错了
来源: 互联网 发布时间:2017-02-15
本文导语: #include #include #include #include #include #include #define BUFSIZE 1024 int main() { int srcfp , destfp ; char buffer[BUFSIZE] ; int num = 0 ; if((srcfp = open("../linux/srcfile",O_RDONLY,0) == -1)) { printf("Source file open failedn") ; ...
#include
#include
#include
#include
#include
#include
#define BUFSIZE 1024
int main()
{
int srcfp , destfp ;
char buffer[BUFSIZE] ;
int num = 0 ;
if((srcfp = open("../linux/srcfile",O_RDONLY,0) == -1))
{
printf("Source file open failedn") ;
exit(1) ;
}
if((destfp = open("../linux/destfile",O_RDWR|O_CREAT,0666) == -1))
{
printf("Destination file open failedn") ;
exit(1) ;
}
printf("start to copy file....n") ;
num = read(srcfp , buffer , BUFSIZE) ;
printf("read bytes is %dn" , num) ;
while(num > 0)
{
printf("%sn" , buffer) ;
if(write(destfp , buffer , num) != num)
{
printf("write error!n") ;
exit(1) ;
}
num = read(srcfp , buffer , BUFSIZE) ;
printf("%dn" , num) ;
}
close(srcfp) ;
close(destfp) ;
return 0 ;
}
#include
#include
#include
#include
#include
#define BUFSIZE 1024
int main()
{
int srcfp , destfp ;
char buffer[BUFSIZE] ;
int num = 0 ;
if((srcfp = open("../linux/srcfile",O_RDONLY,0) == -1))
{
printf("Source file open failedn") ;
exit(1) ;
}
if((destfp = open("../linux/destfile",O_RDWR|O_CREAT,0666) == -1))
{
printf("Destination file open failedn") ;
exit(1) ;
}
printf("start to copy file....n") ;
num = read(srcfp , buffer , BUFSIZE) ;
printf("read bytes is %dn" , num) ;
while(num > 0)
{
printf("%sn" , buffer) ;
if(write(destfp , buffer , num) != num)
{
printf("write error!n") ;
exit(1) ;
}
num = read(srcfp , buffer , BUFSIZE) ;
printf("%dn" , num) ;
}
close(srcfp) ;
close(destfp) ;
return 0 ;
}
|
两个打开文件的语句都写错了,注意括号的位置
if((srcfp = open("../linux/srcfile",O_RDONLY,0) == -1))
==>
if((srcfp = open("../linux/srcfile",O_RDONLY,0)) == -1)
if((srcfp = open("../linux/srcfile",O_RDONLY,0) == -1))
==>
if((srcfp = open("../linux/srcfile",O_RDONLY,0)) == -1)