dirent.h

displaying files from directory

橙三吉。 提交于 2019-12-12 02:19:45
问题 I have a directory name called dir . It contain following files in order 12.07.2013 13.07.2013 14.07.2013 15.07.2013 16.07.2013 17.07.2013 I wrote following C program to display all the files from the directory dir code : #include <stdio.h> #include <string.h> #include <dirent.h> int main (int argc, char *argv[]) { DIR *directory; struct dirent *file; directory = opendir (argv[1]); if (directory != NULL){ while (file = readdir (directory)) printf("FILE : %s \n",file->d_name); (void) closedir

How do I check if a directory is a file or folder? [duplicate]

限于喜欢 提交于 2019-12-11 07:28:00
问题 This question already has answers here : Accessing Directories in C (3 answers) Closed 4 years ago . Okay, so I'm using mingW, and the direct struct has no variables named d_type or stat, d_stat, or dd_stat. I need to know how I can use my direct struct to figure out if what I have is a file or folder. Here is my code. #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> #include <errno.h> #include <vector> #include <string> #include <iostream> using namespace std; /*function...

How can I get only txt files from directory in c?

你。 提交于 2019-12-10 16:13:01
问题 I would like to get names of only *.txt files in given directory, sth like this: #include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> #include <dirent.h> int main(int argc, char **argv) { char *dirFilename = "dir"; DIR *directory = NULL; directory = opendir (dirFilename); if(directory == NULL) return -1; struct dirent *ent; while ((ent = readdir (directory)) != NULL) { if(ent->d_name.extension == "txt") printf ("%s\n", ent->d_name); } if(closedir(directory) < 0)

readdir() 32/64 compatibility issues

那年仲夏 提交于 2019-12-08 19:32:17
问题 I'm trying to get some old legacy code working on new 64-bit systems, and I'm currently stuck. Below is a small C file I'm using to test functionality that exists in the actual program that is currently breaking. #define _POSIX_SOURCE #include <dirent.h> #include <sys/types.h> #undef _POSIX_SOURCE #include <stdio.h> main(){ DIR *dirp; struct dirent *dp; char *const_dir; const_dir = "/any/path/goes/here"; if(!(dirp = opendir(const_dir))) perror("opendir() error"); else{ puts("contents of path:

why does the C readdir man page say to not call free on the static allocated result struct

北城余情 提交于 2019-12-06 03:37:36
问题 $ uname -a Linux crowsnest 2.6.32-28-generic #55-Ubuntu SMP Mon Jan 10 23:42:43 UTC 2011 x86_64 GNU/Linux $ man readdir: DESCRIPTION The readdir() function returns a pointer to a dirent structure representing the next directory entry in the directory stream pointed to by dirp... ..[snip]... The readdir_r() function is a reentrant version of readdir()... ...[snip]... RETURN VALUE On success, readdir() returns a pointer to a dirent structure. (This structure may be statically allocated; do not

why does the C readdir man page say to not call free on the static allocated result struct

怎甘沉沦 提交于 2019-12-04 07:20:18
$ uname -a Linux crowsnest 2.6.32-28-generic #55-Ubuntu SMP Mon Jan 10 23:42:43 UTC 2011 x86_64 GNU/Linux $ man readdir: DESCRIPTION The readdir() function returns a pointer to a dirent structure representing the next directory entry in the directory stream pointed to by dirp... ..[snip]... The readdir_r() function is a reentrant version of readdir()... ...[snip]... RETURN VALUE On success, readdir() returns a pointer to a dirent structure. (This structure may be statically allocated; do not attempt to free(3) it.) If the end of the directory stream is reached, NULL is returned and errno is

stat outputting the wrong values for files in a directory

我是研究僧i 提交于 2019-12-02 03:06:45
问题 I am trying to create a function that will take an inputted directory path (filrOrDir) and output info for each file in the directory: file name, size, and last access date. The program compiles and prints everything. It prints the right file names but, for each file, the size and last access date are wrong. I thought maybe it was because of my variable declarations being in the while loop but I moved them around and still go the same results. Can somebody please give me a hint or a tip on

stat outputting the wrong values for files in a directory

允我心安 提交于 2019-12-02 01:41:53
I am trying to create a function that will take an inputted directory path (filrOrDir) and output info for each file in the directory: file name, size, and last access date. The program compiles and prints everything. It prints the right file names but, for each file, the size and last access date are wrong. I thought maybe it was because of my variable declarations being in the while loop but I moved them around and still go the same results. Can somebody please give me a hint or a tip on what I am doing wrong? Below is my code: void dirInfo(char *fileOrDir) { DIR *d; struct dirent *dir; d =

Accessing Directories in C

落花浮王杯 提交于 2019-11-28 13:05:48
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_name); if(ptr=fopen(dent->d_name,"r")) { print("\tFILE\n"); fclose(ptr); } else printf("\t DIRECTORY\n");

Members of Dirent structure

元气小坏坏 提交于 2019-11-28 06:24:15
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 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 { ino_t d_ino; /* inode number */ off_t d_off; /* offset to the next dirent */ unsigned short d_reclen; /*