c - write a file and make it read only -


i write database students in c under linux. after entering data , after closing program, data saved automatically in text-file. how can make file read only? later add function read data file, program can process data. user opens file can read data has no rights change data in file. in other words how can make saved data secure? here code:

void writedata(){     file * fpw;     fpw=fopen("database.txt","wb");     if(fpw==null){        printf("the file cannot opened");        exit(1);     }     int i;     for(i=0;i<size;i++){         fprintf(fpw,"%s, %s, %d, %s , %s;\n",                (db+i)->lastname,(db+i)->firstname,(db+i)->mnr, (db+i)->subject, (db+i)->nationality);     }     fclose(fpw); } 

i wrote example. here code:

#include <sys/stat.h> #include <stdio.h> int main(int argc, char** argv){     char mode[]="111";     int in=strtol(mode,0,8);     int ret_value=chmod("./file.csv",in);     if(ret_value!=0){         printf("failed change mode");        }    } 

but want program has rw-rights. rest must have read-rights including me terminal-user. how can it? think, try read setuid , use it

use chmod in c change mode of file.

#include <sys/stat.h>  int chmod(const char *path, mode_t mode); 

the following example sets read permissions owner, group, , others.

#include <sys/stat.h>  const char *path; ... chmod(path, s_irusr|s_irgrp|s_iroth); 

if want read permissions, use stat.

#include <sys/stat.h>  int stat(const char * restrict path, struct stat * restrict buf); 

Comments