How to send integer with pipe between two processes!

前端 未结 4 1796
情深已故
情深已故 2021-02-05 12:12

I am trying to send an integer with pipe in a POSIX system but write() function is working for sending string or character data. Is there any way to send integer wi

4条回答
  •  遇见更好的自我
    2021-02-05 12:24

    The safe way is to use snprintf and strtol.

    But if you know both processes were created using the same version of compiler (for example, they're the same executable which forked), you can take advantage of the fact that anything in C can be read or written as an array of char:

    int n = something();
    write(pipe_w, &n, sizeof(n));
    
    int n;
    read(pipe_r, &n, sizeof(n));
    

提交回复
热议问题