How to find a specific file in a folder? - c

So i tried something and everything works fine if the file i'm searching doesn't have an extension in its name (i have a txt file named "instructions" and a fifo file named "myfifo" and they were found).
struct dirent *de=NULL;
DIR *dirr=NULL;
dirr=opendir(".");
while((de=readdir(dirr)) != NULL)
{
printf("%s\n",de->d_name);
if (strcmp(de->d_name,file)==0) printf("Found!\n");
// else printf("kkbesa\n");
}
the variable "file" is declared as char file[100] and i read it from the keyboard.
As i mentioned above, for the files "myfifo" and "instructions" it printed "Found", but when i go for something like "code.c" or "code.exe" it doesn't print anything(Yes, they exist in the current folder).

Related

I can't use input file in my project because Clion seems to not see the file

I recently started using Clion to program in c, but it seems that Clion can't open a txt file for input in my project. I read online that i have to set a proper working directory so that the IDE can see the input file, but every time i tried to change the working directory, nothing changed. I created a simple program to test if the txt file is opened correctly:
#include <stdio.h>
int main()
{
if((fp = fopen("file.txt", "r")) == NULL)
{
printf("Error: file.txt is not present\n");
exit(EXIT_FAILURE);
}
return 0;
}
The program always print the error. I placed the txt in the main folder of this project, which contains:
.idea (folder)
cmake-build-debug (folder)
CmakeLists.txt (file)
main.c (file)
file.txt (the txt file that i want to be open in my program)
I think i have to place file.txt inside the folder that contains the .exe file (cmake-build-debug) but also in this case the output of the program is the same.
You can get the current directory using getcwd to find where the program is looking for file.txt
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("Current working dir: %s\n", cwd);
}

How to rename a file by its inode in Minix mfs?

As a part of an alignement at university I have to modify the function unlink_file located in /usr/src/minix/fs/mfs/link.c, so (under certain conditions) instead of removing files it just changes their name.
I have parent directory's inode, file's inode and its name passed to the function as parameters:
static int unlink_file(dirp, rip, file_name)
struct inode *dirp; /* parent directory of file */
struct inode *rip; /* inode of file, may be NULL too. */
char file_name[MFS_NAME_MAX]; /* name of file to be removed */
I thought of using the rename(2) syscall (which implementation is located in the same file in the function fs_rename), but I need an absolute path of the file in order to do so. Unfortunately, I don't know how to retrieve it from inode structure.
My question is: how can I retrieve an absolute path to the file by its inode? Or is there another way to rename a file from inside the unlink_file function?
notice what fs_rename does with message and that it acquires pointer to inodes.
unlink_file already has pointer to inode of file and pointer to directory where file is located. If you only have to rename it, you can check how fs_rename() acts, when both old_dirp and new_dirp are same
same_pdir == (old_dip == new_dirp); //somewhere in fs_rename()
(bunch of error checks..)
if(same_pdir){
r = search_dir(old_dirp, old_name, NULL, DELETE, IGN_PERM); // this deletes the file from directory
if(r == OK)
(void)search_dir(old_dirp, new_name, &numb, ENTER, IGN_PERM); //this creates file with new_name in the directory
}
Keep in mind this part of code assumes no file with name new_name currently exists in the directory (as in the error checks I've skipped such file gets removed)

rename() only works for directory that program is running in?

I am trying to rename a bunch of files in a user specified directory, but it only seems to be working when the user specifies the directory that the program is running from. For example, when running from the command line:
./a.out . "NewName.txt" will work, while
./a.out .. "NewName.txt" will not work. Is there a reason for this? It's on Linux, by the way.
int main(int argc, char** argv){
char* dirpath = argv[1];
char* newName = argv[2];
DIR *d;
struct dirent *dir;
d = opendir(dirpath);
if (d){
while ((dir = readdir(d)) != NULL){
char* filename = dir->d_name;
if (rename(filename,newName) == 0){
printf("Renaming %s -> %s\n",filename,newName);
} else {
printf("Could not rename %s\n",filename);
}
}
}
closedir(d);
}
I have also tried (while running the program from outside of Desktop):
if (rename("~/Desktop/test.txt","~/Desktop/test2.txt") == 0){
printf("Renaming %s -> %s\n",filename,newName);
} else {
printf("Could not rename %s\n",filename);
}
and it still fails.
While readdir() is reading file names from the other directory, your program's current directory is still in a different location. Unless you prefix the source file name with the path to the directory (and the destination file name too) you're trying to rename non-existent files in the current directory, in general.
In pseudo-code:
dir = opendir(remote_directory)
foreach name from dir
rename "remote_directory/name" to "remote_directory/othername"
end for
Note that the pseudo-code works if 'remote_directory' happens to be ., the current directory; you don't need to special-case that code.
I believe that your main problem is that the result from readdir is just the filename. It doesn't include the directory. You need to paste the directory name and the filename from dir->d_name together in your program.
From the documentation:
The old argument points to the pathname of the file to be renamed.
The new argument points to the new pathname of the file.
If the new argument does not resolve to an existing directory entry for a
file of type directory and the new argument contains at least one non-<slash>
character and ends with one or more trailing <slash> characters after all symbolic
links have been processed, rename() shall fail
Looks like you're not referring to an existing element when you use any path other than '.', which is likely why it's failing.
Check the specific errno value to see why.

How to display all files with dirent and stat structs in C

OK, so I'm trying to display all files within a given path-name.
For the first path I enter, all files display correctly.
However, when I use a sub-directory path as an argument to the function, it only
displays a single executable (there are other files in the sub-directory as a test).
I absolutely cannot figure out why this is happening.
I will post the relevant snippets of code below, any help with this would be greatly
appreciated.
struct dirent *directory;
DIR *pdirectory;
struct stat fileinfo;
pdirectory=opendir(path);
if (pdirectory==NULL)
{
printf("Error: Unable to open directory\n");
exit(0);
}
printf("%s\n",path);
while ((directory=readdir(pdirectory)) != NULL)
{
if (!stat(directory->d_name,&fileinfo))
{
if (S_ISREG(fileinfo.st_mode))
{
printf("File Name: %s\n",directory->d_name);
printf("File Size: %d bytes\n",fileinfo.st_size);
printf("Last Access: %s\n",ctime(&fileinfo.st_atime));
}
}
}
closedir(pdirectory);
}
Your problem is that the stat(2) system call is failing because your not providing him with the complete path to the files located in the opened directory, in the case you are using your program to open a directory other than the current one.
This explains why your code worked only when opening the current directory.
One solution would be to append the path to the directory you're opening to the name of the file contained in the d_name member of the dirent structure such as :
/path/to/my/directory/name_of_the_file
or
path/to/my/directory/name_of_the_file

how to access particular file in folder through file handling in c

I have suppose two text file abc.txt and def.txt in folder "my". I have a programme which directly goes to that folder and search particular file and if that particular file find out then how to access that file's information.
I know how to read write file in C through file handling but I have no idea how to search particular file and after that read that particular file to match particular string in file.
**All these things access through file handling in C.**
So please if any one have any solution I will be thankful for that
Example will be best way to understand .
Thanks in advance
To get a listing of the files in a directory in Linux, you can use the 'opendir', 'readdir' and 'closedir' functions from 'dirent.h'. For example:
#include <dirent.h>
#include <stdio.h>
int ListDir(const char *pDirName)
{
DIR *pDir;
struct dirent *pEntry;
pDir = opendir(pDirName);
if (!pDir)
{
perror("opendir");
return -1;
}
while ((pEntry = readdir(pDir)) != NULL)
{
printf("%s\n", pEntry->d_name);
}
closedir(pDir);
return 0;
}

Resources