Pregunta

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.

Note that the file is open in a mode, not a+. According to various sources, re-positioning operators like fseek() are suppose to be ignored. For a while I was actually getting the third line before the first line after that line was written. If the fseek() line is omitted, the third line actually is written before the header ..

If the fseek() function is left commented (regardless of being in a or a+ mode) the output is as shown in the picture below.

I wrote in a bit of code to see how the output should be written. The text in the file is certainly not what it should be ..

I attempted to use the fseek() function to find the position just before the EOF before each write but to no avail.

I have also noticed that if I use fflush(writeP) then I get the same effect that including the fseek() function would. The file is still out of order as shown, but the third line is no longer before the header line.

What am I missing?

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 < 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;

}

enter image description here

With the fseek() line commented, the output is:

enter image description here

¿Fue útil?

Solución

You do not close writeP the first 3 times when function is called. So, the file is opened by several FILE handles which get closed on exit. The "a" works only for the same FILE handle or when data have reached the disk.

Otros consejos

The problem comes from the fact that you open the same file on every function call, but only close it on the third call. I would suggest moving the file opening and closing logic out of that function, and passing the FILE * handle as an argument to the function; this would also avoid having to hard code the call number on which to close into that function.

So the place where you call the function would look something like this:

FILE *writeP = fopen(QUICKSORT_FILE, "w"); // "a" changed to "w"
if (!writeP) {
    perror(QUICKSORT_FILE);
    exit(1);
}
// perhaps write the header into the file here
for (int i = 0; i < 3; ++i) {
    // do the quicksort
    write_quicksort_results(writeP, …);
}
(void) fclose(writeP);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top