The issue arises with fopen(), stating that permission is denied. I have tried to input all the modes however this was unsuccessful. Same when adding all the writing modes. I am attempting to open a file to with which i can write. Thanks to anyone who helps
if(filename != NULL && redirectArrow == true){
mode_t modes = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
int fd = open(filename, O_WRONLY | O_CREAT , S_IWUSR | S_IWGRP | S_IWOTH); //opening file in write only, and creating file if it does not exist
if(fd == -1){
perror("Failed to create and open the file\n");
exit(3);
}
Related
Im making a shell and working on output redirection, specifically appending to files. Problem is, the following code always overwrites the contents of the file and does not simply append.
int val = -1;
//Flag issue?
val = open(destination, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
if (val != -1) {
close(1);
dup(val);
close(val);
}
I have this function that utilizes open to set i/o redirection:
void setOutput(char * buffer){
int file = open(buffer, O_WRONLY || O_CREAT, S_IWUSR);
if(file < 0){ printf("error opening %s for output\n", buffer); }
if(dup2(file, 1) < 0){ printf("error with dup2 opening %s for output\n", buffer); }
}
When I run it, it works fine for files that are already defined but returns -1 when it receives a non-created file. Not sure why
You need to change the following
int file = open(buffer, O_WRONLY || O_CREAT, S_IWUSR);
To
int file = open(buffer, O_WRONLY | O_CREAT, S_IWUSR);
Format :
int open( char *filename, int access, int permission );
access : Should be provided as a bit wise OR operator, that means using | not || which is logical OR
My problem is simple, when i try to confirm if the function wrote on the file, nothing shows up, the file is there, but there's nothing inside the file, where should be 1024 times the string i wanted.
int escreve1x( const char* path , const char* cadeia )
int fd = open( path, O_CREAT, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH );
int i;
printf("%s\n", cadeia);
for ( i=0 ; i<=1024 ; i++ )
{
write( fd, cadeia, 10);
}
return 0 ;
1-Add "O_RDWR" flag for making fd writable.
2-Always debug the result of write syscall;
int fd = open( path, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH );
if(fd<0) {
printf("Open failed\n");
return 1;
}
int i;
printf("%s\n", cadeia);
for ( i=0 ; i<=1024 ; i++ ) {
int status=write( fd, cadeia, 10);
if(status<0) {
printf("Write failed");
}
}
return 0;
If it says write has failed then check errno.
I don't know why redirection doesn't work in the shell I have written. Here's my code"
int i;
for (i=1; !args[i];i++)
{
if (args[i]== ">")
{
printf("argv[i] %s %d \n", args[i], i);
int out;
// out = open("out", O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
out=open("out", O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU);
int fdl=dup2(out,1);
close(out);
execvp(args[0],args);
}
}
Also here's the error I receive :
mysh> ls
basic_shell basic_shell.c~ fork fork_2 fork_cp.c
basic_shell.c basic_shell_OK.c fork_1 fork.c
mysh> ls > file
ls: cannot access >: No such file or directory
ls: cannot access file: No such file or directory
Please let me know what's wrong?
If args is an array of char*, then this condition
if (args[i]== ">")
does not do what you think it does. It compares the pointers and not what they point to. To compare string you have to use strcmp.
On a mac running 10.8 i am trying to open a serial port.
ls /dev/cu* returns:
/dev/cu.Bluetooth-Modem /dev/cu.Bluetooth-PDA-Sync /dev/cu.usbserial-A1009TT7
i can see the port is there but when i try to open it i get Undefined error: 0(0). This is my code i use to open the port.
char *path = "/dev/cu.usbserial-A1009TT7";
open(path , O_RDWR | O_NOCTTY | O_NONBLOCK); // open the port
if (file == -1) {
printf("Error opening port : %s(%d).\n", strerror(errno), errno);
close(file);
return -1;
}
anyone have any idea why the port wont open?
thanks in advance.
Whoops! You meant to type this:
file = open(path , O_RDWR | O_NOCTTY | O_NONBLOCK);
^^^^^^^
Also, there is no need to close a file descriptor that isn't open.
if (file == -1) {
printf(...);
// close(file); Completely unnecessary. It's not valid!
return -1;
}