c - C中的read()和write()有什么错误?

标签 c

<分区>

这是一个小的C程序,用于测试客户端和服务器程序,以便客户端向客户端发送一个整数。服务器将该数字乘以 10 并将整数 * 10 返回给客户端。将整数写入 FIFO 时。 到目前为止,这是我的代码:

#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

main (void)
{
  int fda;      
  int fdb;      
  int number;
  int outputnumber;



  if((fda=open("FIFO_to_server", O_WRONLY))<0)
       printf("cant open fifo to write");

  if((fdb=open("FIFO_to_client", O_RDONLY))<0)
       printf("cant open fifo to read");

   printf("Client: Please enter an integer: ");
   scanf("%d", &number);


   write(fda, number, sizeof(number));
   printf("\nClient: Got the number sent, now waiting for response ");
   read(fdb, outputnumber, sizeof(outputnumber));
   printf("\nClient: received from server %s", outputnumber);

   close(fda);
   close(fdb);

   printf ("\nall done!\n");

 }

编译后出现错误:

-bash-3.2$ gcc clientHw.c -o client
 clientHw.c: In function `main':
 clientHw.c:36: warning: passing arg 2 of `write' makes pointer from integer 
 without a cast
 clientHw.c:38: warning: passing arg 2 of `read' makes pointer from integer 
 without a cast

最佳答案

您必须像这样传递变量的地址:

write(fda, &number, sizeof(number));
...
read(fdb, &outputnumber, sizeof(outputnumber));

关于c - C中的read()和write()有什么错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52487807/

相关文章:

c - 将函数指针分配给另一个函数内的函数

分配很多基本数据类型会导致内存泄漏,你能避免吗?

我可以只使用 percolateDown 实现堆吗?

c++ - 进入子进程时gdb中断

c - 如何使用 stdio.h C 库读取文本文件并将内容写入另一个文本文件

c - 这段代码(在描述中)如何工作?

c - C中的多件商品价格计算

c - epoll_wait with timerfd api 用于非阻塞读取

c - 哪个C标准支持命名可变参数宏?

java - JNA 将 Java String 映射到 PCWSTR