I'm looking to list and store the contents of a directory in a struct using C on Windows.
I got a problem with stat(), I don't really understand this line
if (statut.st_mode & S_IFDIR)
So I want to understand how it works for checking if it's a directory or a file?
stat() retrieves a block of information describing the specified file. Directories are also files. A directory can be thought of as a file that contains other files.
So, in the file's st_mode, you can see whether the current file is actually a directory by checking for the presence of the S_IFDIR bit.
Related
I searched online to find that how to change the directory name? But I found nothing. All I found was rename() that was changing filenames. I want to change a directory name. Linux uses mv command for renaming a directory but if the directory contains large files then moving will take time more right? I want to do this in C.
On every file system I can think of, renaming a directory doesn't actually involve moving the files to a new location but rather changing a record in the file system.
To move a directory on Windows:
if(!MoveFileW(L"C:\\Path\\OldDirName", L"C:\\Path\\NewDirName"))
{
// Operation failed
}
On *nix:
if(rename("/path/olddirname", "/path/newdirname") != 0)
{
// Operation failed
}
Wrapper macros:
#ifdef __linux__
#define MvDir(old, new) (rename((old), (new)) != 0)
#elif defined(WINVER)
#define MvDir(old, new) !(MoveFileW((L ## old), (L ## new))
#endif
This also automatically wraps the success-test of the expression into a 1/0 value for you.
...how to change the directory name? ...All I found was rename() that was changing filenames.
No, that's incorrect. rename is totally fine for renaming directories, also.
Linux uses mv command for renaming a directory but if the directory contains large files then moving will take time more right?
The mv command is typically implemented using rename, which is very fast. Renaming a directory does not necessarily require moving (or doing anything with) the files in it.
If you're doing a more complicated rename, like trying to rename a/b/c to d/e/f, and if this ends up trying to move things onto a different mounted filesystem, then a simple directory rename won't work, and yes, actually "moving" (actually copying) of files would be required. In that case, the rename() call will fail, so you'll know. (errno should contain EXDEV.)
In the cross-device case, the mv command will do the expensive work of copying files, but only if it has to.
If you use rename from a C program, you'll get a quick rename for renames on the same filesystem, and an error for renames that attempt to cross filesystems, and you'll never get any expensive copies unless you code them up yourself.
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
I need to create a directory for a file. The easiest path would be to create a directory with the same name as the file, and put the file there.
Is there any case where a valid file name would be an invalid directory name?
The directory will of course be created on the same system as where the file name is valid.
So basically I'm asking if there is any system where there are different limitations on directory names and file names.
As the number of characters in a path is limited on some filesystems, any file that uses more than half the limit of path characters would cause problems if put into a directory of the same name.
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.
i am tring to add a new feature to bash
and the job need a func to get the directory name from the given inode value
so how to got this work? i mean if there's some builtin func in bash's source code
if not , any help will be accept
This is impossible without searching through the filesystem for a dir that contains a file with the given inode. Filenames are named references to inodes with no backlinks.
List the directory and get the inode of .. then list that and find the name of the file there that has the inode you are looking for.