Home > Back-end >  Why can't I open my file using open method?
Why can't I open my file using open method?

Time:12-16

Why can't I open my file using open method?

this is my source code:

char* dest;
char cwd1[256];
if (getcwd(cwd1, sizeof(cwd1)) == NULL)
perror("getcwd() error\n");
else
    dest=cwd1;    
char* destPlus=strcat(dest,"/");
char*myPathName =strcat(destPlus,fileName);
printf("the path name1: %s \n",myPathName);
char* outputPathName=myPathName;
int d;
// FILE* fd;
if(append)
    d= open(outputPathName, O_WRONLY, O_APPEND | O_CREAT|O_WRONLY|O_TRUNC);
else
    d= open(outputPathName, O_WRONLY,O_CREAT|O_WRONLY|O_TRUNC);
if(d<0){
    printf("hello, there's an error with your file, plz check it out\n");
    return;
}

in this code d will be negative, why? Note: the path is true I check it, the error Number is 2

CodePudding user response:

Both uses of open are wrong:

if(append)
    d= open(outputPathName, O_WRONLY, O_APPEND | O_CREAT|O_WRONLY|O_TRUNC);
else
    d= open(outputPathName, O_WRONLY,O_CREAT|O_WRONLY|O_TRUNC);

open() takes three arguments - the const char *path, the int oflag, and the variable argument mode_t mode, which is only used for newly-created files to specify the most-permissive access mode that can be used for the file (as modified by the process's umask setting). mode only needs to be supplied when O_CREAT is set in in the oflag flags variable.

You are misusing both the oflag and mode argument.

A proper use of open() would be something like this:

if(append)
    d= open(outputPathName, O_WRONLY|O_CREAT|O_APPEND, 0644 );
else
    d= open(outputPathName, O_WRONLY|O_CREAT, 0644 );

Per [POSIX open() documentation](https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/functions/open.htm

SYNOPSIS

#include <sys/stat.h>
#include <fcntl.h>

int open(const char *path, int oflag, ...);
int openat(int fd, const char *path, int oflag, ...);

DESCRIPTION

The open() function shall establish the connection between a file and a file descriptor. It shall create an open file description that refers to a file and a file descriptor that refers to that open file description. The file descriptor is used by other I/O functions to refer to that file. The path argument points to a pathname naming the file.

The open() function shall return a file descriptor for the named file, allocated as described in File Descriptor Allocation. The open file description is new, and therefore the file descriptor shall not share it with any other process in the system. The FD_CLOEXEC file descriptor flag associated with the new file descriptor shall be cleared unless the O_CLOEXEC flag is set in oflag.

The file offset used to mark the current position within the file shall be set to the beginning of the file.

The file status flags and file access modes of the open file description shall be set according to the value of oflag.

Values for oflag are constructed by a bitwise-inclusive OR of flags from the following list, defined in <fcntl.h>. Applications shall specify exactly one of the first five values (file access modes) below in the value of oflag:

O_EXEC Open for execute only (non-directory files). The result is unspecified if this flag is applied to a directory.

O_RDONLY Open for reading only.

...

O_CREAT If the file exists, this flag has no effect except as noted under O_EXCL below. Otherwise, if O_DIRECTORY is not set the file shall be created as a regular file; the user ID of the file shall be set to the effective user ID of the process; the group ID of the file shall be set to the group ID of the file's parent directory or to the effective group ID of the process; and the access permission bits (see <sys/stat.h>) of the file mode shall be set to the value of the argument following the oflag argument taken as type mode_t modified as follows: a bitwise AND is performed on the file-mode bits and the corresponding bits in the complement of the process' file mode creation mask. Thus, all bits in the file mode whose corresponding bit in the file mode creation mask is set are cleared. When bits other than the file permission bits are set, the effect is unspecified. The argument following the oflag argument does not affect whether the file is open for reading, writing, or for both. Implementations shall provide a way to initialize the file's group ID to the group ID of the parent directory. Implementations may, but need not, provide an implementation-defined way to initialize the file's group ID to the effective group ID of the calling process.

  • Related