c - Write to file not visible before close; fflush(stdout) ineffective -


i'm having issues writing file whilst having delay in while loop. here's snippet:

void main(int){    file * fp = null;    sprintf(filename, "log%i.msg", set_id);    fp = fopen(filename, "w+");     fprintf(fp, "file started\n");    while(1){       fprintf(fp, "%i %u %s\n", someint, someunsigned, somestring);       fflush(stdout);        sleep(5); // commenting out line work    }    fclose(fp);    return 1; } 

running code gives me output file of 0 bytes nothing in whilst sleep taking effect, although file have expected content when code finishes running. however, when remove sleep(5); line, print correctly. i've searched already, i've found needs flushed, (though apparently incorrectly). doing wrong?

you're flushing stdout. need flush file.

change

fflush(stdout) 

to

fflush(fp) 

in terms of why sleep() appears impact whether contents visible in file: without present, you're writing file @ higher rate, fill in-memory buffer , flush disk faster. if patient enough, though, you'd see contents on-disk sleep() present.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -