【问题标题】:Writing integers to file descriptor using write?使用write将整数写入文件描述符?
【发布时间】:2012-03-24 17:18:00
【问题描述】:

我想使用 write (http://linux.about.com/library/cmd/blcmdl2_write.htm) 将整数 1 写入文件描述符的第一个字节,并将 0x35 写入第二个字节,但我得到以下信息当我尝试以下操作时发出警告:

write(fd, 1, 1);
write(fd, 0x35, 1);


source.c:29: warning: passing argument 2 of ‘write’ makes pointer from integer without a cast
source.c:30: warning: passing argument 2 of ‘write’ makes pointer from integer without a cast

【问题讨论】:

    标签: c unix file-descriptor


    【解决方案1】:

    第二个参数应该是一个指向缓冲区的指针,你可以这样做:

    char a = 1;
    write(fd, &a, 1);
    

    甚至更简单:

    char buff[] = {1, 0x35};
    write(fd, buff, sizeof(buff));
    

    【讨论】:

      【解决方案2】:

      您需要传递一个地址,因此您需要某种形式的变量。

      如果您只需要一个字符:

      char c = 1;
      write(fd, &c, 1);
      c = 0x35;
      write(fd, &c, 1);
      

      或者使用数组(这个一般比较常见):

      char data[2] = { 0x01, 0x35 };
      write(fd, data, 2);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-20
        • 1970-01-01
        • 2019-06-20
        • 1970-01-01
        • 1970-01-01
        • 2010-09-20
        • 1970-01-01
        相关资源
        最近更新 更多