I am new with open function in C. For now to open a file named file I am doing the following :
open("file", O_CREAT|O_RDWR|O_APPEND, S_IRUSR | S_IWUSR)
If the file doesn't exist it does create the file and it gives the right to read the file and to write at the end of it.
Yet the problem is that if the file already exists it doesn't erase its content. What is the option I should use here ?
O_APPEND means you'll be appending to the file if it exists. You should use O_TRUNC instead:
open("file", O_CREAT|O_RDWR|O_TRUNC, S_IRUSR | S_IWUSR)
/* Here ---------------------^ */
Related
i would like to check if a file exist, delete content if it exists or create it if not.
I have tried :
open("screenshot.bmp", O_CREAT | O_RDWR | O_TRUNC);
But the file don't update if it already exists, if it doesn't the file is created correctly.
if ((fd = open("screenshot.bmp", O_CREAT, S_IRWXU)) > -1)
return (-1);
close (fd);
if ((fd = open("screenshot.bmp", O_TRUNC)) > -1)
return (-1);
But the file looks corrupted/empty after that (it should be filled by the rest of my code)
I also tried other ways.
Thanks for help !
Try using FILE *fd = fopen("screenshot.bmp", "w");
Accorsing to tutorialspoint:
FILE *fopen(const char *filename, const char *mode)
"w"
Creates an empty file for writing. If a file with the same name already exists, its content is erased and the file is considered as a new empty file.
Update:
OP says fopen(...) isn't allowed, but...
According to the docs you can achieve the same result as the fopen(...) call using:
open (filename, O_WRONLY | O_CREAT | O_TRUNC, mode)
For example (from the docs):
The following example opens the file /tmp/file, either by creating it (if it does not already exist), or by truncating its length to 0 (if it does exist). In the former case, if the call creates a new file, the access permission bits in the file mode of the file are set to permit reading and writing by the owner, and to permit reading only by group members and others.
If the call to open() is successful, the file is opened for writing.
#include <fcntl.h>
...
int fd;
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
char *filename = "/tmp/file";
...
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, mode);
...
Is is possible to get a file descriptor from a file path in C? And if not, what is the standard way of generating a file descriptor in C?
See below for what I'm trying to do with a file descriptor:
char file[] = "/tmp/run"
int file_descriptor = /* fill in code to get file descriptor */
fchmod(file_descriptor, S_IRUSR | S_IWUSR | S_IXUSR)
That's exactly what the POSIX open() function does: It gives you a file descriptor that refers to the path you passed in.
(Note that neither open nor file descriptors in general are part of standard C.)
I am trying to open a file for both read and write operations.
If the file is already there, it should append. (I want to be able to write to it, and maybe read from it later)
However, if the file is there, I cannot append to it (I get a permission denied: cannot create file)
int main()
{
int file;
file = open("redirect.txt", O_RDWR | O_APPEND | O_CREAT, 777);
if(!(file == -1)) //edited per comment
{
close(file);
}
else
perror("File could not be created\n");
return 0;
}
This only opens a new file if it does not exist, but does not append to an existing file if it does exist.
You're forgetting that the mode parameter to open() must be in octal. This will work:
file = open("redirect.txt", O_RDWR | O_APPEND | O_CREAT, 0777);
As zwol also mentioned, it's generally a good idea to create files with 0666 (since they don't need to be executable).
I created a file with the flag O_CREAT, but when I tried to open the created "out.txt" with Notepad. It said "cannot open this file" or something like "access denied".
fd = open("out.txt", O_CREAT);
You must close your file using close-call. Otherwise its contents won't be flushed and the file not written to disk. Besides you might be telling what you want to do on the file.
fd = open("out.txt", O_WRONLY | O_CREAT); //write to the file
//write to file
close(fd); //might check return value
See Wikipedia on this.
I'm working on a linux C project and I'm having trouble working with file descriptors.
I have an orphan file descriptor (the file was open()'d then unlink()'d but the fd is still good) that has write-only permission. The original backing file had full permissions (created with S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), but alas the file was opened with O_WRONLY. Is it possible to duplicate the file descriptor and change the copy to O_RDWR?
psudo-code:
//open orphan file
int fd = open(fname, O_WRONLY, ...)
unlink(fname)
//fd is still good, but I can't read from it
//...
//I want to be able to read from orphan file
int fd2 = dup(fd)
//----change fd2 to read/write???----
Thanks in advance!
-Andrew
No, there is no POSIX function to change the open mode. You will need to open it in read / write mode. Since you are created a temporary file, though, I strongly recommend that you use mkstemp. That function properly opens the file in read/write mode and unlinks it. Most importantly, it avoids a race condition in naming and creating the file, thereby avoiding a vulnerability in the creation of temporary files.
int fd = open(fname, O_WRONLY, ...)
int fd_ro = open(fname, O_RDONLY, ...)
unlink(fname)
{ write to fd }
close (fd);
read or execute(!) fd_ro