rename a file using C function rename() - c

I'm using a Mac. I need to rename a file in the /Library/Application Support/AppName/filename.aiff
This is the system library and not /User/username/Library...
I'm using the rename(old name, new name) function. This function doesn't work (even though it doesn't return an error) if I place the file in the /Library/Application Support/AppName directory but it works properly if I place the file, for example, in /User/username/Documents/filename.aiff.
I don't know what the problem is. Any help would be appreciated. Thanks!

You don't own the directory you're trying to move files into:
/Users/Username/... is a user owned directory, so you're allowed to manipulate files there.
/Library is not a user owned directory.
In order to manipulate files in a non-user owned directory you would need elevated permissions. Instead of using /Library you should be using ~/Library, which is the user owned directory. ~/Library is the shorthand name for /Users/Username/Library.

If the rename is working fine in other path means the problem should be related to permission to access file or with the path searched for file.

Related

How to change the current directory to a designated directory using chdir() in c?

EDIT
The existed directory is not necessarily a sub-directory of the home directory. It can be a sub-directory of a sub-directory of the home directory.
End of EDIT
I am reading a user input (e.g., cd existedDirectory) to change the current directory to the "existedDirectory". I know that
chdir(getenv("HOME"));
can change the current directory to home directory, so I tried the following methods:
chdir(getenv("/existedDirectory"));
chdir(getenv("existedDirectory"));
chdir(getenv("~/existedDirectory"));
chdir("/existedDirectory");
chdir("existedDirectory");
chdir("~/existedDirectory");
Nothing worked. Any help will be greatly appreciated.
chdir takes a string argument with a path to change to. It does no other special handling (environment vars or home or anything else), so you if you want any of those things, you need to build a string to pass into it.
chdir("/existingDirectory");
which change to and existing directory in the root directory -- it has an absolute path. So it has to be one of the directories you see when you run ls /.
chdir("exisitingDirectory");
will change to an existing subdirectory of the current director.
If you want to change to a subdirectory of your home directory, you need something like:
char path[PATH_MAX];
sprintf(path, "%s/%s", getenv("HOME"), "subdir");
chdir(path);
chdir() in C only affects the process, which calls it. Your shell and your program are two different processes, which run separately. So, the environmental variable $PWD, which indicates the working directory, will only be changed for your application. Check this resource, too.
Note:chdir() doesn’t change the working directory of current shell. Because when the program is executed in the shell, the shell follows fork on exec mechanism. So, it doesn’t affect the current shell.

why my Xcode doesn't find the .txt files that I create?

my program seems to "see" only the .txt files which he creates with the fopen("file.txt","w") function. I changed the working directory and put added the file to the project, but if I create the file.txt the program can't see it. the only way is to edit the one he creates with the open function
You just have a problem concerning the current directory, it is not what you expect.
When you create "file.txt" it is created in the current directory because the path is not specified, so you can open it after because it is where the program look at by default.
{edit add}
You are under MacOS probably, if you start the program by hand from a shell like /Applications/......./prog the current directory is the current directory where you are in the shell, but if you start it through its icon etc it will depends on the installation directory

How can I prevent AccessDeniedException during Files.walkFileTree?

I'm trying to use Java 1.7 nio. When I call
Files.walkFileTree(source[i], tc);
where source[i] is any folder on my Windows file system and tc is a SimpleFileVisitor, I get an java.nio.file.AccessDeniedException. I've checked the folder and the folder contents and ensured that read, write, and execute are allowed for all users.
I can access the file fine using File. Why does walkFileTree have a problem accessing the file?
I found the solution to this issue. I overrode the visitFile method and it was incorrectly referencing a file path.

Relative path in C file handling

I need to read file in my program so while providing path I want to give relative paths because all files to be opened will be in some folder within current folder.
I tried this:
FILE *f=fopen("./abc/p.txt","r")
abc is folder withing current folder, but fopen returns NULL. How to do this thing?
This comes from either one of those:
. or ./abc/ is not readable or traversable
./abc/p.txt is not readable
./abc/p.txt does not exist
./abc/p.txt is a broken link
Look at errno to know what's the real problem.
this will run:
FILE *f=fopen("...\\abc\\p.txt","r");

FreeBsd: Allow users of the same group have automatic read/write access to files and directories created by them

In our FreeBSD-environment where we have one server that acts as a file-server, we have a problem that our system administrator says can not be fixed.
All our files resides in a directory and we all have access to that directory, its sub-directories and files. The problem is that once a user in our group creates a file or directory, we have to chmod that directory or file to change the rights so that others in our group can access, read, write and delete. These are not files or sub-directories inside our home-directories, but in a directory where we are supposed to work with them on a daily basis.
Finding it difficult to believe that there is no good solution, I would request that someone assist me with a solution.
I think what you want is a setgid bit on the directories and umask. Then newly created there files and directories will have proper group and proper permissions to let others read abd write them.
find /your-files-are-rooted-here -type d -print0 | xargs -0 chmod ug+rw,g+s
and set umask to 002 (or whatever is appropriate). And, of course, you may want to fix permissions for existing files (the command above only takes care of directories).
One place to but the umask setting is "/etc/bashrc". Find "umask". Change "umask = 022" to "umask = 002". After doing this, when a new file created, every one in the same group with the file owner can write in this new file.
Note that this only works for files created from the shell, specifically bash.

Resources