Can I fopen with shared read write?

user2533527 picture user2533527 · Jul 16, 2013 · Viewed 7.4k times · Source

Can I create a file in C that will be accessible at any time in user mode?

I mean like

    zwcreatefile(...shareread||sharewrite...)

Can I make it with fopen in user mode ?

I want my log file to be shared read write so when my program is running I can still open it and view the logs while my program still writing to there.

Answer

user2533527 picture user2533527 · Jul 16, 2013
#include <stdlib.h>
#include <stdio.h>
#include <share.h>
#include <stdlib.h>

FILE *FPlogHigh; 

int main(int argc, char **argv)
{   
    FPlogHigh = _fsopen("filename.txt", "a+",_SH_DENYWR);
    if (FPlogHigh)
    {
        fprintf(FPlogHigh,"\nHello log started\n");
        fclose(FPlogHigh);
    }
    ...
}

I opening and closing the file handle each time I want to write there. It allows me to access the log file from any other program. I also can not change log in other program while this 1 is writing to log file but as it closes FCLOSE(); I can edit a+ stands for append it writes to the end of the file each times it opens.