|
楼主 |
发表于 2003-4-18 15:35:40
|
显示全部楼层
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
int rlt;
extern int errno;
char *zz="adasdfads11";
char *dd;
char *pp;
int length=0;
struct stat *status=(struct stat*)malloc(sizeof(struct stat));
rlt=open("hehe",O_CREAT|O_RDWR);
write(rlt,zz,strlen(zz));
close(rlt);
rlt=open("hehe",O_CREAT|O_RDONLY);
fstat(rlt,status);
length=lseek(rlt,0,SEEK_SET);
length=lseek(rlt,0,SEEK_END)-length;
dd=(char*)malloc(status->st_size+1);
pp=(char*)malloc(length+1);
memset((void*)dd,'\0',status->st_size);
memset((void*)pp,'\0',length);
fsync(rlt);
if(read(rlt,dd,status->st_size)==-1)
{
printf("error read dd\n");
free(status);
free(dd);
free(pp);
exit(1);
}
fsync(rlt);
if(read(rlt,pp,length)==-1)
{
printf("error read pp\n");
free(dd);
free(pp);
free(status);
exit(2);
}
printf("%d\n",status->st_size);
printf("%d\n",length);
printf("dd:%s\n",dd);
printf("pp:%s\n",pp);
free(status);
free(pp);
free(dd);
close(rlt);
} |
|