C 读写系统调用始终返回1
我需要将一个文件的内容复制到另一个文件。但问题是我得到的读写返回值总是1。但缓冲区中的数据是读取的。有人能解释一下这里出了什么问题吗? 我正在使用gcc编译器C 读写系统调用始终返回1,c,linux,C,Linux,我需要将一个文件的内容复制到另一个文件。但问题是我得到的读写返回值总是1。但缓冲区中的数据是读取的。有人能解释一下这里出了什么问题吗? 我正在使用gcc编译器 #include<stdio.h> #include <fcntl.h> #include <errno.h> #include <unistd.h> #include<string.h> int copy_file_to_file(int,int); int main() {
#include<stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include<string.h>
int copy_file_to_file(int,int);
int main()
{
int src_fd,dst_fd;
if ( (src_fd = open("source.txt",O_RDONLY)) == -1 )
{
printf("file opening error");
return -1;
}
if( (dst_fd = open("destination.txt",O_WRONLY | O_CREAT,0644)) == -1 )
{
printf("destination file opening error\n");
return -1;
}
copy_file_to_file(src_fd,dst_fd);
close(src_fd);
close(dst_fd);
return 0;
}
int copy_file_to_file(int source_fd,int dest_fd)
{
int byte_read, byte_write;
char buf[10];
while((byte_read = read(source_fd, buf, 10)>0) )
{
byte_write=write(dest_fd, buf, byte_read);
printf("buf=%s byte_read = %d byte_write=%d \n",buf,byte_read,byte_write);
}
return 0;
}
我在工作
字节读=1字节写=1
被解释为br=read>0
修正:
顺便说一句,在现实生活中,你需要几十KB的缓冲区,而不是10字节!对这只是一个示例程序。我以前是复制块的,不是10字节的。
buf=Hello world
while((byte_read = read(source_fd, buf, 10)>0) )
while((byte_read = read(source_fd, buf, 10)) > 0)