Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple read() operations on the same file
    primarykey
    data
    text
    <p>I'm looking at using the read() function to read in entire data structures, each of which will be of the same type as any other, but with different data, then place them into a linked list. For some reason I can't seem to find any specific information on how to terminate a loop which would include <code>read(fp, &amp;tmp, sizeof(struct foo))</code> followed by <code>new_node(tmp)</code>.</p> <p>I would like to be able to simply read until EOF, but I'm not aware of how to go about determining with the read() function where EOF is. Obviously, I could use a workaround with the write() function where I would include the number of structures in the file before writing, and then terminate the read function when I reach that number, but that seems a bit clunky, and avoids the original idea of knowing when the file terminates.</p> <p>FOLLOW-UP:</p> <p>I appreciate the assistance, and I've implemented what I've seen. Unfortunately, I believe I may be reading in the wrong information. The pertinent code:</p> <pre><code>struct test_t{ int data; char buf[LEN]; struct test_t * next; }; struct test_t * new_node(struct test_t node, struct test_t * tail) { struct test_t * tmp = NULL; if(!(tmp = malloc(sizeof(struct test_t)))) return NULL; tmp-&gt;data = node.data; strcpy(tmp-&gt;buf, node.buf); tmp-&gt;next = NULL; if(tail) tail-&gt;next = tmp; return tmp; } ... while(read(fd, &amp;tmp, sizeof(struct test_t)) == sizeof(struct test_t)){ printf("%d, %s\n", tmp.data, tmp.buf); tail = new_node(tmp, tail); if(head == NULL) head = tail; printf("%d, %s\n", tail-&gt;data, tail-&gt;buf); } ... fd = open("test.txt", O_WRONLY | O_CREAT, 0666); iter = head; while(iter){ printf("%d\n", write(fd, &amp;iter, sizeof(struct test_t))); printf("%d, %s\n", iter-&gt;data, iter-&gt;buf); iter = iter-&gt;next; } </code></pre> <p>This is the output from the write loop:</p> <pre><code>112 1, a 112 2, b 112 3, c 112 4, d 112 5, e </code></pre> <p>The file is saved in binary, but I can make out enough to know that only the tail seems to be written, five times. I'm not sure why that is.</p> <p>The output for the diagnostic printf's in the read loop is:</p> <pre><code>23728144, 23728144, 23728272, 23728272, 23728400, 23728400, 23728528, 23728528, 23728656, 23728656, </code></pre> <p>The output makes me think it's putting the value of the next pointer into the data int. Any idea why: 1) I might be write()ing the same node five times in a row? 2) I am getting gibberish when I read()?</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.
 

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