- 论坛徽章:
- 0
|
第一段代码如下:
int main()
{
int fd;
int flength = 10;
char *mapped = NULL;
fd = open("src.txt", O_RDONLY);
mapped=(char *)mmap(0,flength,PROT_READ,MAP_PRIVATE,fd,0);
if( mapped == MAP_FAILED){
printf("map failed");
return 0;
}
lseek(fd, 5, SEEK_SET);
mapped = (char *)mmap(mapped, flength, PROT_READ, MAP_PRIVATE, fd, 0);
printf("out is --->%s\n", mapped);
munmap(mapped, flength);
close(fd);
return 0;
}
src源文件是 1234567890, 我想输出 67890, 可每次都是全部输出, 怎么回事呢?
第二段代码, 1.txt中是1M的文件;
总是段错误, 代码如下:
int main()
{
int fd;
char *mapped_mem, * p;
int flength = 10;
fd = open("1.txt", O_RDONLY);
int off_set = 0;
for(int i = 0; i<1024; i++)
{
mapped_mem = (char *)mmap(mapped_mem, flength, PROT_READ, MAP_PRIVATE, fd, off_set);
if( mapped_mem == MAP_FAILED){
printf("map failed");
return 0;
}
off_set += strlen(mapped_mem);
munmap(mapped_mem, strlen(mapped_mem));
printf("out is--->%d\n", strlen(mapped_mem));
}
close(fd);
return 0;
}
该怎么改呢 这两段代码 多谢指教 |
|