dirent.h

opendir() in C Programming

家住魔仙堡 提交于 2021-02-19 16:32:13
问题 I'm a beginner and I am making a code about getting the directory of a file, but I have something that I don't understand. What's the meaning of "./" in DS = opendir ("./"); I have searched a lot of sites about C programming, but nothing gave me a good explanation. I have to present my code soon, forcing me to explain every single line of my code. Please help me. Thanks! 回答1: ./ is a relative path which is relative to the current working directory of your process. In computing, the working

How to qsort a dirent in C

元气小坏坏 提交于 2021-02-11 15:26:54
问题 Brand new to C and finding it confusing. What I really want to know about, is taking two separate pieces of code and getting them to work together. Here's some code that simply lists the contents of the current directory: #include <stdio.h> #include <sys/types.h> #include <dirent.h> int main (void) { DIR *dp; struct dirent *ep; dp = opendir ("./"); if (dp != NULL) { while (ep = readdir (dp)) puts (ep->d_name); (void) closedir (dp); } else perror ("Couldn't open the directory"); return 0; }

How to qsort a dirent in C

我只是一个虾纸丫 提交于 2021-02-11 15:25:56
问题 Brand new to C and finding it confusing. What I really want to know about, is taking two separate pieces of code and getting them to work together. Here's some code that simply lists the contents of the current directory: #include <stdio.h> #include <sys/types.h> #include <dirent.h> int main (void) { DIR *dp; struct dirent *ep; dp = opendir ("./"); if (dp != NULL) { while (ep = readdir (dp)) puts (ep->d_name); (void) closedir (dp); } else perror ("Couldn't open the directory"); return 0; }

lstat: can't access files in another directory

你说的曾经没有我的故事 提交于 2021-02-05 08:09:31
问题 I'm trying to write ls-alike program that produces output like ls -l with permissions, owners, time and name of the file. It works good if I pass . (or nothing), so it works with the current directory. But if I pass any other directory in or out of the current one, perror says it "can't access" the files. Please, help me figure out what prevents lstat from accessing files in another dirs. I use gcc and a text editor, no IDE, started learning to use gdb (tried to debug but didn't find

dirent not working with unicode

微笑、不失礼 提交于 2021-01-29 16:26:36
问题 i try to count files in folder, but readdir function skip on files that contains unicode characters. I am using dirent, in c. int filecount(char* path) { int file_Count=0; DIR* dirp; struct dirent * entry; dirp = opendir(path); while((entry=readdir(dirp)) !=NULL) { if(entry->d_type==DT_REG) { ++file_Count; } } closedir(dirp); return file_Count; } 回答1: Testing on Mac OS X 10.9.1 Mavericks, I adapted your code into the following complete program: #include <dirent.h> #include <stdio.h> static

Ubuntu listing up files recursively, detecting sym-links

两盒软妹~` 提交于 2019-12-25 02:28:59
问题 I'm trying to list up my local file-system recursively using dirent.h . In order to prevent from following sym-links, I'm using the sys/stat.h header. In the following you can find my SSCCE program. /** * coding: utf-8 * * Copyright (C) 2013, Niklas Rosenstein * * listdir.c - List up directories and file-content recursively. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <dirent.h> #include <sys/stat.h> void list_dir(const char* directory_name) { DIR* directory_handle

How to use dirent.h correctly

陌路散爱 提交于 2019-12-24 08:27:38
问题 I am new to C++ and I am experimenting with the dirent.h header to manipulate directory entries. The following little app compiles but pukes after you supple a directory name. Can someone give me a hint? The int quit is there to provide a while loop. I removed the loop in an attempt to isolate my problem. thanks! #include <iostream> #include <dirent.h> using namespace std; int main() { char *dirname = 0; DIR *pd = 0; struct dirent *pdirent = 0; int quit = 1; cout<< "Enter a directory path to

Accessing Directories in C

会有一股神秘感。 提交于 2019-12-17 20:42:03
问题 The program is to open a directory and to display the name of the files... i.e if there is a file..it should say FILE....else DIRECTORY.. but the program displays all the files as directory.. Could anyone pls check the code for any errors....thnx #include<stdio.h> #include<dirent.h> #define DIR_path "root/test" main() { DIR *dir; dir=opendir(DIR_PATH); printf("THe files inside the directory :: \n"); struct dirent *dent; if(dir!=NULL) { while((dent=readdir(dir))) { FILE *ptr; printf(dent->d

Members of Dirent structure

不打扰是莪最后的温柔 提交于 2019-12-17 18:04:42
问题 I have started working with dirent.h library and I came across a very useful member of "struct dirent" structer which struct dirent *p->d_name in my book. But unfortunatly it doesn't states any other members of this structure; I was wondering what else are the members of this structure and what are they used for? Regards 回答1: The structure, struct dirent refers to directory entry. http://www.gnu.org/software/libc/manual/html_node/Directory-Entries.html In linux it is defined as: struct dirent

How do i check if a file is a regular file?

随声附和 提交于 2019-12-12 10:59:14
问题 How do i check in C++ if a file is a regular file (and is not a directory, a pipe, etc.)? I need a function isFile(). DIR *dp; struct dirent *dirp; while ((dirp = readdir(dp)) != NULL) { if ( isFile(dirp)) { cout << "IS A FILE!" << endl; i++; } I've tried comparing dirp->d_type with (unsigned char)0x8, but it seems not portable through differents systems. 回答1: You need to call stat(2) on the file, and then use the S_ISREG macro on st_mode. Something like (adapted from this answer): #include