Currently the the file gets created and overwritten. I am trying to get it so that if the file already exists it just exits the program. open must be used.
if ((dest = open(argv[2], O_WRONLY | O_CREAT, 0644)) == -1) {
printf("Error File %s exists", argv[2]);
return 3;
}
Just use O_EXCL:
O_EXCL Ensure that this call creates the file: if this flag is
specified in conjunction with O_CREAT, and pathname already exists,
then open() will fail.
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);
...
My following program, which copies a file, won't allow me to copy files because of "permission denied". However, I gave it permissions.
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
int fdinput, fdoutput; //file pointers
char arrbuf[5000]; //size of what can be read in file
ssize_t bytesR, bytesW;//number of what input returns
mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IROTH |S_IXOTH ;
fdinput = open(argv[1], O_RDONLY); //pointing to read file
fdoutput = open(argv[2], O_WRONLY);//pointing to write file
if(fdinput == -1){
perror("the source file cant be opened");
return 1;
}
if(fdoutput == -1){
perror("the written file cant be opened");
return 2;
}
while((bytesR = read(fdinput, arrbuf, sizeof arrbuf)) > 0){
bytesW = write(fdoutput, arrbuf, (ssize_t) bytesR);
}
close(fdinput);
close(fdoutput);
return 0;
}
The problem is in the call to the system call open() for the destination file (i.e.: the file to be created as a result of the copy):
fdoutput = open(argv[2], O_WRONLY);
Making possible the creation of the destination file
First, the call above to open() opens the file with the given name by argv[2], only if it already exits. Otherwise, the system call fails (errno is set to ENOENT) and perror() produces:
the written file cant be opened: No such file or directory
In order to create the file if it does not exist yet, the O_CREAT flag has to ORed together with O_WRONLY.
Truncating an already existing destination file
If the destination file already exist you surely want to truncate the length of that already existing file to zero at the moment of open()ing. That can by achieved by ORing the O_TRUNC flag together with the other flags.
Providing the permissions for the file to be created
Let's look at the open() system call's prototype:
int open(const char *path, int oflag, ...);
The ... at the end is to specify a kind of optional argument. That argument is used by open() only when a new file is being created. It provides the mode bits to be applied for the file to be created. This is not exactly the permissions for the file to be created, but it is strongly related to them (for more info see: file mode creation mask).
You created mode of type mode_t but just forgot to pass it to open().
With all exposed above in mind, your call should look like:
fdoutput = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, mode);
File is there and having json data inside it. I want to know length of file.but when i try below code it but size remains 0.
int file_contentl_Len = 0;
int fd_0 ;
fd_0 = open(FILE_PATH_CONFIG_0, O_WRONLY | O_TRUNC | O_CREAT, 0644);
if(fd_0 < 0)
{
printf("\r\nError opening Config file %s: %s\n",FILE_PATH_CONFIG_0, strerror(errno));
return -1;
}
struct stat buf;
fstat(fd_0, &buf);
file_contentl_Len = buf.st_size;
printf("\r\nConfig file %s content length: %d\r\n", FILE_PATH_CONFIG_0, file_contentl_Len);
You opened the file for writing with truncation, creating it if necessary — O_WRONLY | O_TRUNC | O_CREAT.
The size of zero tells you the truncation worked, or the file was created empty.
If you wanted to read what was in the file, use O_RDONLY instead. Or use O_RDWR and think carefully about whether to allow the file to be created.
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.