- 论坛徽章:
- 0
|
嗯,是这样的……
下面是我写的一段代码
/*
* this program is about the useage of read ang write
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
int main(void)
{
int fdsrc,fdnull,fdtmp,numbytes;
int flag = O_CREAT | O_TRUNC | O_RDONLY;
char buf[10];
if (fdsrc = open("fopen.c",O_RDONLY,0644)<0) {
perror ("open fopen.c"); /*注意perror函数必须的参数必须用双引号括住*/
exit(EXIT_FAILURE);
}
if (fdnull = open("/dev/null",O_RDONLY)<0) {
perror("open /dev/null");
close(fdsrc);
exit(EXIT_FAILURE);
}
if (fdtmp = open("tmp",flag,0644)<0) {
perror("open tmp");
close(fdnull);
close(fdsrc);
exit(EXIT_FAILURE);
}
while((numbytes = read(fdsrc,buf,10))>0 ) {
if (write(fdnull,buf,10) < 0) {
perror ("write /dev/null");
}
if (write(fdtmp,buf,numbytes) < 0) {
perror ("write tmp");
}
}
close(fdsrc);
close(fdnull);
close(fdtmp);
exit(EXIT_SUCCESS);
}
可是我运行后,它好像陷入了死循环一般,我猜是不是while循环那块出了问题,还请各位高手指点指点,小弟我这厢有礼了…… |
|