linux中c语言read中fd,在Linux下通过read读文件出现的问题

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#define infile "temp.dat"

#define outfile "outfile.dat"

#define length 20

void translation()

{

int creatf;

int infd, outfd;

char buf[50];

if ((creatf = open(infile, O_WRONLY | O_TRUNC | O_CREAT, 10700)) == -1)

{

printf("ERROR, OPEN READ FILE FAILED: \n", sys_errlist[errno]);

exit(255);

}

else

{

printf("Create Successful\n");

}

strcpy(buf, "Hello");

if (write(creatf, buf, strlen(buf)) != strlen(buf))

{

printf("ERROR, WRITE FILE FAILED: \n", sys_errlist[errno]);

exit(255);

}

else

{

printf("Write Successful\n");

}

close(creatf);

if (infd = open(infile, O_RDONLY)==-1)

{

printf("ERROR, OPEN READ FILE FAILED: \n", sys_errlist[errno]);

exit(255);

}

else

{

printf("INFILE Open Success\n");

}

if (outfd = open(outfile, O_WRONLY | O_TRUNC | O_CREAT, 10700)==-1)

{

printf("ERROR, OPEN WRITE FILE FAILED: \n", sys_errlist[errno]);

exit(255);

}

else

{

printf("OUTFILE Open Success\n");

}

int count;

while((count = read(infd, buf, sizeof(buf))) > 0)            //在这一行就出现程序就停下来了

{

if (write(outfd, buf, count) != count)

{

printf("ERROR, WRITE FILE FAILED: \n", sys_errlist[errno]);

exit(255);

}

else

{

printf("%s", buf);

}

}

close(infd);

close(outfd);

}

void main()

{

translation();

}

在读文件时,程序就停止了,本人按回车后才继续显示下一步,但是和预想中的不一样!

1708ded53d53a2a4216475b4aabddae7.png

上图表示在此处停住了

4ff1d9ffd391411887a3bae056d0217f.png

上图表示停住后,按下回车键时,断断续续的出现的东西(文件里面只写了”Hello”)

本人想问一下问题到底出在了什么地方?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值