Note that there are some explanatory texts on larger screens.

plurals
  1. POdispatch_write() and dispatch_read() usage
    primarykey
    data
    text
    <p>I'm just playing with some GCD functions for writing and reading data to files. Two of these functions are <code>dispatch_write()</code> and <code>dispatch_read()</code>, which allow one to write and read data to a file descriptor without having to setup a new <code>dispatch_io_t</code> channel.</p> <p>So, I have the following code:</p> <pre><code>#import &lt;dispatch/dispatch.h&gt; #import &lt;stdio.h&gt; #import &lt;unistd.h&gt; int main() { dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); int intbuffer[] = { 1, 2, 3, 4 }; dispatch_data_t data = dispatch_data_create(intbuffer, 4 * sizeof(int), queue, NULL); // Write dispatch_fd_t fd = open("data.dat", O_RDWR); printf("FD: %d\n", fd); dispatch_write(fd, data, queue,^(dispatch_data_t d, int e) { printf("Written %zu bytes!\n", dispatch_data_get_size(d)); printf("\tError: %d\n", e); }); close(fd); // Read fd = open("data.dat", O_RDWR); dispatch_read(fd, 4 * sizeof(int), queue, ^(dispatch_data_t d, int e) { printf("Read %zu bytes!\n", dispatch_data_get_size(d)); printf("\tError: %d\n", e); }); close(fd); // Exit confirmation getchar(); return 0; } </code></pre> <p>with which I'm attempting to write a 4-integer array to a file and, after that, to read it back. I previously created <code>data.dat</code> with the <code>touch</code> command and anyone has full access to it (<code>sudo chmod 777 data.dat</code>).</p> <p>When I execute this code, it seems <code>data.dat</code> gets open successfully, since the program prints out <code>FD: 3</code>, which is a valid file descriptor, but <code>dispatch_write</code> doesn't write anything to it, since I get:</p> <pre><code>Written 0 bytes! Error: 9 Read 0 bytes! Error: 9 </code></pre> <p>Error 9 is the code for a <code>EBADF</code> error, but, again, 3 is a valid file descriptor.</p> <p>So, what am I doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload