当前位置: 技术问答>linux和unix
一段代码,麻烦大家看一下!!
来源: 互联网 发布时间:2015-03-11
本文导语: #include #include #include #include #include #include #define BUFFER_SIZE 108 #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]; ...
#include
#include
#include
#include
#include
#include
#define BUFFER_SIZE 108
#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 filenamena",argv[0]);
exit(1);
}
from_fd=open(DEVICE_NAME,O_RDWR);
if(!to_fd)
{
printf("errno is %dn",errno);
exit(1);
}
if((to_fd=open(argv[1],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))==-1)
{
fprintf(stderr,"Open %s Error:%sn",argv[2],strerror(errno));
exit(1);
}
lseek(to_fd,MY_OFFSET,SEEK_SET);
bytes_read=read(from_fd,buffer,BUFFER_SIZE);
ptr=buffer;
bytes_write=write(to_fd,ptr,bytes_read);
close(from_fd);
close(to_fd);
exit(0);
}
运行后竟将软盘第250扇区之后的内容全读出来了,可我只想要读108个字节,怎么办?
#include
#include
#include
#include
#include
#define BUFFER_SIZE 108
#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 filenamena",argv[0]);
exit(1);
}
from_fd=open(DEVICE_NAME,O_RDWR);
if(!to_fd)
{
printf("errno is %dn",errno);
exit(1);
}
if((to_fd=open(argv[1],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))==-1)
{
fprintf(stderr,"Open %s Error:%sn",argv[2],strerror(errno));
exit(1);
}
lseek(to_fd,MY_OFFSET,SEEK_SET);
bytes_read=read(from_fd,buffer,BUFFER_SIZE);
ptr=buffer;
bytes_write=write(to_fd,ptr,bytes_read);
close(from_fd);
close(to_fd);
exit(0);
}
运行后竟将软盘第250扇区之后的内容全读出来了,可我只想要读108个字节,怎么办?
|
程序看过了,不过我是在windows 下上网,所以没能试验。
你把bytes_read=read(from_fd,buffer,BUFFER_SIZE);
ptr=buffer;
bytes_write=write(to_fd,ptr,bytes_read);
改为
bytes_read=read(from_fd,buffer,BUFFER_SIZE);
bytes_write=write(to_fd,buffer,bytes_read);
试试看。:)
你把bytes_read=read(from_fd,buffer,BUFFER_SIZE);
ptr=buffer;
bytes_write=write(to_fd,ptr,bytes_read);
改为
bytes_read=read(from_fd,buffer,BUFFER_SIZE);
bytes_write=write(to_fd,buffer,bytes_read);
试试看。:)