Note that there are some explanatory texts on larger screens.

plurals
  1. PORandom writing order to output file?
    text
    copied!<p>I've written a program that writes information to a file. The output to the file is not in the order that I am expecting. I have a header line and three additional lines with numerical information. The header comes first following by the third, first, and second lines. </p> <p>Note that the file is open in <code>a</code> mode, not <code>a+</code>. According to various sources, re-positioning operators like <code>fseek()</code> are suppose to be ignored. For a while I was actually getting the third line <strong><em>before</em></strong> the first line <strong><em>after</em></strong> that line was written. If the <code>fseek()</code> line is omitted, the third line actually is written before the header ..</p> <p>If the <code>fseek()</code> function is left commented (regardless of being in <code>a</code> or <code>a+</code> mode) the output is as shown in the picture below. </p> <p>I wrote in a bit of code to see how the output <em>should</em> be written. The text in the file is certainly not what it should be .. </p> <p>I attempted to use the <code>fseek()</code> function to find the position just before the EOF before each write but to no avail. </p> <p><strong>I have also noticed that if I use <code>fflush(writeP)</code> then I get the same effect that including the <code>fseek()</code> function would. The file is still out of order as shown, but the third line is no longer before the header line.</strong></p> <p>What am I missing? </p> <pre><code>void quickSortHelper(int* num, long startTime, long endTime, int size){ FILE *writeP = fopen(QUICKSORT_FILE, "a"); if(writeP == NULL){ fputs("Error opening file\n", stderr); exit(1); } static int times = 0; long deltaT; //change in time if(size &lt; STEPSIZE){//first time, write header to file printf("Writing header!\n"); fprintf(writeP, " --- QUICKSORT ---\nCOUNT\tTIME\tMEDIAN\n"); } deltaT = (clock() - startTime)/(CLOCKS_PER_SEC/1000); //fflush(writeP); fseek(writeP, -1, SEEK_END); printf("Writing: %d\t%ld\t%d\n", size, deltaT, findMedian(num, size)); fprintf(writeP, "%d\t%ld\t%d\n", size, deltaT, findMedian(num, size)); if(++times == 3) fclose(writeP); return; </code></pre> <p>}</p> <p><img src="https://i.stack.imgur.com/FOpbz.jpg" alt="enter image description here"></p> <p>With the <code>fseek()</code> line commented, the output is:</p> <p><img src="https://i.stack.imgur.com/RUJ5Q.jpg" alt="enter image description here"></p>
 

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