日期:2014-05-16  浏览次数:20689 次

复制文件内容出错了
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#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 failed\n") ;
exit(1) ;
}

if((destfp = open("../linux/destfile",O_RDWR|O_CREAT,0666) == -1))
{
printf("Destination file open failed\n") ;
exit(1) ;
}

printf("start to copy file....\n") ;

num = read(srcfp , buffer , BUFSIZE) ;

printf("read bytes is %d\n" , num) ;

while(num > 0)
{
printf("%s\n" , buffer) ;

if(write(destfp , buffer , num) != num)
{
printf("write error!\n") ;
exit(1) ;
}

num = read(srcfp , buffer , BUFSIZE) ;
printf("%d\n" , 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)