Whenever I use open I get the permission denied error. But when I use fopen it opens the file fine. What is wrong with my code?
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
char *filename = "dataread.txt";
rec = open(filename ,O_WRONLY | O_CREAT | O_TRUNC,mode);
if(rec == -1)
{
perror("\nopen error 1:");
exit(1);
}
Error:
open error 1:: Permission denied
With fopen I don’t get this error.
I added an
int rec=0;var declaration and the necessary includes files and then compiled your code.It runs with no errors as a normal user in my Fedora 15 laptop. Check the dir/file permissions you are running this on, the problems does not seem to be in the code.