c - read() 和 write() 未按预期工作

标签 c stdout

这是我作为简单实验的一部分所做的事情;

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int fun()
{
 int d=10;
 write(1,(int*)d,sizeof(d));
}

int main()
{ 
 int x;
 fun();
 read(1,(int*)x,4);
 printf("x=%d\n",x);
 return 0;
}

如您所见,我正在尝试通过 IO 流(IO 文件)stdout 访问 fun() 函数中 d 的值;我也尝试使用 stdin ,但问题是 x 值没有改变。 (我在在线 IDE 中尝试过。)但根据我的想法,它应该更改为 d 的值。

谁能帮我看看为什么它不这样做?

最佳答案

读写需要指针。在您的情况下,它是 &d&x。将 int 转换为 int* 通常没有意义。

关于c - read() 和 write() 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42106716/

相关文章:

c++ - iostream 链接器错误

Python Popen().stdout.read() 挂起

c - Linux:捕获已运行进程的输出(纯 C 语言!)

c - 在 C 程序中列出全局变量

c - 通过loadrunner处理批处理文件

gcc - gcc从stdin读取gnu readline()的编译错误

python - 来自已完成子进程的 stdout.read() 有时会返回空值?

java - 为什么我没有得到任何输出?并且有一个InputMismatchException

c - c 中替代用户输入的替代函数

c - 函数参数列表后的变量声明