Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I fopen with shared read write?

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.

like image 489
user2533527 Avatar asked Jul 15 '13 23:07

user2533527


1 Answers

#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.

like image 162
user2533527 Avatar answered Sep 24 '22 23:09

user2533527