For the following snippet of code, I get an error:
Unable to open file: No such file or directory
redirect_ptr is char** And I have tried printing redirect_ptr[0], it prints it correctly. Any idea where the problem might lie?
if ((in_fd = open(redirect_ptr[0], O_RDWR | O_CREAT)) == -1) {
perror("Unable to open file");
return -1;
}
When you create a file, open() needs an additional argument, the permission bits on the file to create. You need to do e.g.
if ((in_fd = open(redirect_ptr[0], O_RDWR | O_CREAT, 0644) == -1)
That might not be the cause of the error you get, however if the error is "No such file or directory", then that's precisely what is wrong, you program cannot find the file.
Perhaps you have some non-printable characters in the file name, or the name ends with a space or newline or similar, or you spelled the name wrong, or have the wrong case, or the path is a relative path that doesn't match the file based on the current working directory of your process.
It's often helpful to print the filename inside a pair of '' , so you can see if there's some whitespace that should not be there. add a
printf("Filename: '%s'\n",redirect_ptr[0]);
to your code. And if it looks good, do a ls -l on the filename it prints out, standing in the working directory of the process.