我正在努力unix system calls
。我想string
从standard input
using中读取read()
,然后使用write()
.
我可以到open
文件,read
从string
,standard input
但不能到write
文件。
我的代码是:
#include <unistd.h> // to remove WARNINGS LIKE warning: implicit declaration of function ‘read’ , warning: implicit declaration of function ‘write’
#include<fcntl.h> /* defines options flags */
#include<sys/types.h> /* defines types used by sys/stat.h */
#include<sys/stat.h> /* defines S_IREAD & S_IWRITE */
#include<stdio.h>
int main(void)
{
int fd,readd;
char *buf[1024];
fd = open("myfile",O_RDWR);
if(fd != -1)
printf("open error\n");
else
{
// read i\p from stdin , and write it to myfile.txt
if((readd=read(0,buf,sizeof(buf)))<0)
printf("read error\n");
else
{
printf("\n%s",buf);
printf("\n%d",readd);
if(write(fd,buf,readd) != readd)
printf("write error\n");
}
}
return 0;
}
输出是
write error
它工作正常,如果我write
要string
standard output
问题 :
1)有什么问题write()
?
2)我想在行尾包含换行符\n
。怎么可能通过standard input
?