opendir

Delete files from a specific folder in C

假如想象 提交于 2019-12-02 12:43:47
I'm trying to delete files from a specifc folder. My deleteFile() function only deletes on its home folder, not on /tmp folder which is what I need. I tried the same approach as my displayDIR() function to change directory but I can't figure out how to make it work. I use cygwin as compiler. void deleteFile() { int status; char filetodelete[25]; printf("\n \t **Delete File**\n"); displayDIR(); printf("\n\tChoose the name of the file to delete:\t"); scanf("%s", filetodelete); status = remove(filetodelete); if( status == 0 ) printf("%s file deleted successfully.\n", filetodelete); else { printf(

Which is faster: glob() or opendir()

心已入冬 提交于 2019-11-29 11:31:08
Which is faster between glob() and opendir(), for reading around 1-2K file(s)? Ben Rowe http://code2design.com/forums/glob_vs_opendir Obviously opendir() should be (and is) quicker as it opens the directory handler and lets you iterate. Because glob() has to parse the first argument it's going to take some more time (plus glob handles recursive directories so it'll scan subdirs, which will add to the execution time. Gordon glob and opendir do different things. glob finds pathnames matching a pattern and returns these in an array, while opendir returns a directory handle only. To get the same

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");

Select random file using OPENDIR()

自作多情 提交于 2019-11-28 13:05:47
问题 I have tried: function random_pic($dir = '../myfolder') { $files = opendir($dir . '/*.*'); $file = array_rand($files); return $files[$file]; } This function works using glob() but not opendir. This returns a failed to open directory error. I guess opendir cannot accept things like *.* ? Is it possible to select all files in a folder and randomly choose one? 回答1: The opendir() function wont return a list of files/folders. It will only open a handle that can be used by closedir() , readdir() or

PHP - opendir on another server

柔情痞子 提交于 2019-11-28 06:20:23
问题 I'm kinda new to PHP. I've got two different hosts and I want my php page in one of them to show me a directory listing of the other. I know how to work with opendir() on the same host but is it possible to use it to get access to another machine? Thanks in advance 回答1: You could use PHP's FTP Capabilities to remotely connect to the server and get a directory listing: // set up basic connection $conn_id = ftp_connect('otherserver.example.com'); // login with username and password $login

Which is faster: glob() or opendir()

你。 提交于 2019-11-28 05:11:43
问题 Which is faster between glob() and opendir(), for reading around 1-2K file(s)? 回答1: http://code2design.com/forums/glob_vs_opendir Obviously opendir() should be (and is) quicker as it opens the directory handler and lets you iterate. Because glob() has to parse the first argument it's going to take some more time (plus glob handles recursive directories so it'll scan subdirs, which will add to the execution time. 回答2: glob and opendir do different things. glob finds pathnames matching a

PHP opendir() to list folders only

孤街醉人 提交于 2019-11-27 13:50:51
问题 I would like to use opendir() to list only the folders in a particular folder (i.e. /www/site/). I would like to exclude files from the list as well at the '.' and '..' folders that appear on a linux folder listing. How would I go about doing this? 回答1: Check out the PHP docs for readdir(). It includes an example for exactly this. For completeness: <?php if ($handle = opendir('.')) { $blacklist = array('.', '..', 'somedir', 'somefile.php'); while (false !== ($file = readdir($handle))) { if (

Get filenames of images in a directory

寵の児 提交于 2019-11-27 06:08:43
问题 What should be done to get titles (eg abc.jpg) of images from a folder/directory using PHP and storing them in an array. For example: a[0] = 'ac.jpg' a[1] = 'zxy.gif' etc. I will be using the array in a slide show. 回答1: It's certainly possible. Have a look at the documentation for opendir and push every file to a result array. If you're using PHP5, have a look at DirectoryIterator. It is a much smoother and cleaner way to traverse the contents of a directory! EDIT: Building on opendir : $dir

C faster way to check if a directory exists

我是研究僧i 提交于 2019-11-27 02:36:10
问题 I'm using opendir function to check if a directory exists. The problem is that I'm using it on a massive loop and it's inflating the ram used by my app. What is the best (fastest) way to check if a directory exists in C? What is the best (fastest) way to create it if doesn't exists? 回答1: You could call mkdir() . If the directory does not exist then it will be created and 0 will be returned. If the directory exists then -1 will be returned and errno will be set to EEXIST . 回答2: Consider using

Checking if a dir. entry returned by readdir is a directory, link or file. dent->d_type isn't showing the type

醉酒当歌 提交于 2019-11-27 02:10:19
I am making a program which is run in a Linux shell, and accepts an argument (a directory), and displays all the files in the directory, along with their type. Output should be like this: << ./Program testDirectory Dir directory1 lnk linkprogram.c reg file.txt If no argument is made, it uses the current directory. Here is my code: #include <stdio.h> #include <dirent.h> #include <sys/stat.h> int main(int argc, char *argv[]) { struct stat info; DIR *dirp; struct dirent* dent; //If no args if (argc == 1) { argv[1] = "."; dirp = opendir(argv[1]); // specify directory here: "." is the "current