|
发表于 2003-6-1 08:30:40
|
显示全部楼层
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/errno.h>
#include <unistd.h>
#define BUFFER_SIZE 512
#define DEVICE_NAME "/dev/fd0"
#define MY_OFFSET (250*512)
int main (int argc, char **argv)
{
int from_fd,to_fd;
int bytes_read,bytes_write;
char buffer[BUFFER_SIZE];
char *ptr;
if(argc!=2)
{
printf("%s need input filename\n\a",argv[0]);
exit(1);
}
from_fd=open(DEVICE_NAME,O_RDWR);
if(!to_fd)
~~~~~u should compare the from_fd here, right? make sure u know what is the return value here from 'open'.
{
printf("errno is %d\n",errno);
exit(1);
}
if((to_fd=open(argv[1],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))==-1)
{
fprintf(stderr,"Open %s Error:%s\n",argv[2],strerror(errno));
exit(1);
}
lseek(to_fd,MY_OFFSET,SEEK_SET);
~~~~why u lseek to_fd?
bytes_read=read(from_fd,buffer,BUFFER_SIZE);
ptr=buffer;
bytes_write=write(to_fd,ptr,bytes_read);
~~~~can u check u byte_read value before u use it?
close(from_fd);
close(to_fd);
exit(0);
}
这是我写的一段代码,可是读出来老是不止512字节,请问怎样才能让它读出我想要的字节数?
~~~i do not know where u can judge how many u read. |
|