为什么 write(1, lol, 1);
行在 main
函数中不起作用?它只是空白并返回值 0 表示程序结束,但是当我调用函数 filecopy(0,1)
时,它可以工作吗?
编辑:我包含了“syscall.h”
int main(void)
{
///// filecopy(0,1)
char lol = 'D';
write(1, lol, 1);
return 0;
}
void filecopy(int from, int to)
{
int n;
int buf[100];
while((n=read(from, buf, 100)) > 0)
write(to, buf, n);
}
最佳答案
在您的 filecopy()
函数中,您正确调用了 write()
。
write(to, buf, n); //buf is a pointer.
在您的main()
代码中,问题出在该行
write(1, lol, 1); // lol is of type char, it's not an address.
write()
的第二个参数应该是一个void *
。更改为
write(1, &lol, 1);
强烈建议在编译器中启用警告,并查看并修复编译器发出的警告。
关于c - 为什么标准输出的 write() 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28398398/