ITPub博客

首页 > > > 求高手帮助。。。。。。。dup问题

求高手帮助。。。。。。。dup问题

作者:yesye 时间:2019-11-06 14:09:54 0 删除 编辑

#include <stdio.h>

#include <stdlib.h>

#include <fcntl.h>

#include <unistd.h>

int main()

{

int fd = open("a.txt",O_RDWR);

if(fd == -1)

{

perror("打开文件失败");

exit(-1);

}

int fd2 = dup(fd);

printf("fd=%d,fd2=%d\n",fd,fd2);

write(fd,"1",1);

write(fd2,"2",1);

int fd3 = dup2(fd,100);

printf("fd3 = %d\n",fd3);

write(100,"ok",fd3);

int cur = lseek(fd3,2,SEEK_SET);

printf("cur=%d\n",cur);

char ch[100] = {};

if(read(fd3,ch,sizeof(ch))<0)

{

perror("读取文件失败");

}

printf("%s\n",ch);

}

运行结果是

fd=3,fd2=4

fd3 = 100

cur=0

12ok

记过没有什么问题,但是文件里出现了问题,貌似文件里的内容不是12ok,还有乱码现象

文件里的内容为:

12okcur=%d

读取文件失败;0����L0���p�����L����N����zR

请高手指点,为什么文件里会有那写乱码问题???????而且那些乱码还读不出来???

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2041/viewspace-3140397/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 这是什么问题~
请登录后发表评论 登录
全部评论

注册时间:2019-09-06

  • 博文量
    139
  • 访问量
    9684