With fopen, by setting it to w will automatically clear the file for me. However now I'm trying to do the same thing with open,
int file = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
This won't guarantee that the file is empty and write at the beginning(?). How can I achieve that?
Add O_TRUNC - Initially clear all data from the file.
O_TRUNC flag truncates the file when opening. It can be used.
int file = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
Related
Below code and the quoted statement are from Linux System Programming by Robert Love.
I don't understand how are the modes in the first and the second code examples the same.
From the first example, I calculated the mode values as
USR -> 2+1 = 3
GRP -> 2+1 = 3
OTH -> 1 = 1
So my expectation was 0331, but it the book says it's 0664.
What's wrong in my logic?
Copied from the book below:
int fd;
fd = open (file, O_WRONLY | O_CREAT | O_TRUNC, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IROTH);
if(fd == -1)
/* error */
Trading portability (in theory at least) for readability, we could
have written the following, to identical effect:
int fd;
fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0664);
if(fd == -1)
/*error*/
For whichever reason, you have chosen to use the wrong values for the constants.
Instead of S_IRUSR being 0400, you decided it was 0200.
Instead of S_IWUSR being 0200, you decided it was 0100.
etc
If you use the correct values for the constants, you will get the same result.
if I have a client descriptor as follows:
int fileDescriptor = open("myfile.txt", O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
how can I edit this statement so that, if I want to open it (double click) I will be asked for the system password?
The access mode bits O_TRUNC and O_APPEND are for system call open()'s 2nd parameter.
They shouldn't be used together, I guess, because they conflict, or 1 will override another, I think.
But I saw following in <The linux programming interface> page 73, is that a bad way?
d = open("w.log", O_WRONLY | O_CREAT | O_TRUNC | O_APPEND,S_IRUSR | S_IWUSR);
I have problem I want to open a file with .out extension or if it not exist then it should create it with permissions to read and write and by opening file I want to erase his content
variable = open( "file.out", O_RDWR | O_CREAT, S_IRWXO, O_TRUNC )
I used this command but it always fails I think I have bad flags as far I know:
O_RDWR is to open file with write and read permission
O_CREAT it creates file if not exist
S_IRWXO is used by O_CREAT to make the file readable and writable
O_TRUNC erase all data from file
open doesn't take 4 arguments - only 2 or 3. All of the O_ flags should be OR'ed together in the second argument. Also, S_IRWXO will give permissions only to others, not to the owner or group owner. You meant S_IRWXU | S_IRWXG | S_IRWXO probably. But I'd rather just see an octal number; the macros aren't more readable.
variable = open( "file.out", O_RDWR | O_CREAT | O_TRUNC, 0777 )
If you want read/write/execute permission for the current user/owner use: S_IRWXU:
S_IRWXO
Read, write, and search or execute permission for users other than the
file owner. S_IRWXO is the bitwise inclusive-OR of S_IROTH, S_IWOTH,
and S_IXOTH.
S_IRWXU
Read, write, and search, or execute, for the file owner; S_IRWXG is the
bitwise inclusive-OR of S_IRUSR, S_IWUSR, and S_IXUSR.
Doing open( "file.out", O_RDWR | O_CREAT, S_IRWXU, O_TRUNC ); and ls -l gives:
-rwx------. 1 perreal perreal 0 Apr 29 15:52 file.out
I have opened a text file named "pranav" in O_APPEND | O_CREAT mode as shown below:
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
main()
{
//FILE Descriptor fdes
/*Open file pranav.txt in write-only mode,
O_CREAT creates file if it does not exist*/
int fdes = open("pranav.txt",O_APPEND | O_CREAT );
//Error returns -1
if(fdes!=-1)
{
//To write on file
if((write(fdes,"Pranav",6))== -1)
write(2,"File_Writing_Error",18);
//To print on screen
else
write(1,"Done",4);
}
else
{
//Print "error" on screen
write(2,"File_Opening_Error",18);
}
close(fdes);
}
In O_APPEND mode it executes the write(2,"File_Writing_Error",18); statement, thus not able to write "Pranav" on file, but this error does not occur and program successfully runs if I use O_WRONLY mode
Documentation for open says, that you must give exactly one of the flags O_RDONLY, O_WRONLY and O_RDWR and that you can use any combination of the other flags like O_APPEND and O_CREAT.
You did not provide O_WRONLY in addition to O_APPEND and O_CREAT. My guess is that O_RDONLY is 0, so when not giving one of the access flags you end up with O_RDONLY and thus cannot write to the file.
So the correct code should be:
mode_t mode = S_IRWXU | SIRWXG; // or any other mode
int fdes = open("pranav.txt", O_APPEND | O_CREAT | O_WRONLY, mode);
Please note the additional parameter mode which is required if the flags included O_CREAT or O_TMPFILE.