I am new to c++. I need to create a program that prints all the files directories and subdirectories. For example, if user enters Local Disk D. It prints all the files in Local Disk D. Currently, I am using netbeans and I manage to print all the files if I insert command 'find "D:\"' in the terminal. This coding is using Dirent.
My problem is, is there any way I can run this program without opening the terminal? If i run usual way, it automatically prints all the files of where I put the project. Or is there any way to call back the printed files in terminal to become output?
Please help.
/*
* An example demonstrating recursive directory traversal.
*
* Compile this file with Visual Studio 2008 project vs2008.sln and run
* the produced command in console with a directory name argument. For
* example, command
*
* find "C:\Program Files"
*
* might produce a listing with thousands of entries such as
*
* c:\Program Files/7-Zip/7-zip.chm
* c:\Program Files/7-Zip/7-zip.dll
* c:\Program Files/7-Zip/7z.dll
* c:\Program Files/Adobe/Reader 10.0/Reader/logsession.dll
* c:\Program Files/Adobe/Reader 10.0/Reader/LogTransport2.exe
* c:\Program Files/Windows NT/Accessories/wordpad.exe
* c:\Program Files/Windows NT/Accessories/write.wpc
*
* The find command provided by this file is only an example. That is,
* the command does not provide options to restrict the output to certain
* files as the Linux version does.
*
* Copyright (C) 2006-2012 Toni Ronkko
* This file is part of dirent. Dirent may be freely distributed
* under the MIT license. For all details and documentation, see
* https://github.com/tronkko/dirent
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
staticint find_directory (constchar *dirname);
int
main(int argc, char *argv[])
{
int i;
int ok;
/* For each directory in command line */
i = 1;
while (i < argc)
{
ok = find_directory (argv[i]);
if (!ok)
{
exit (EXIT_FAILURE);
}
i++;
}
/* List current working directory if no arguments on command line */
if (argc == 1)
{
find_directory (".");
}
return EXIT_SUCCESS;
}
/* Find files and subdirectories recursively */
staticint
find_directory(constchar *dirname)
{
DIR *dir;
char buffer[PATH_MAX + 2];
char *p = buffer;
constchar *src;
char *end = &buffer[PATH_MAX];
int ok;
/* Copy directory name to buffer */
src = dirname;
while (p < end && *src != '\0') {
*p++ = *src++;
}
*p = '\0';
/* Open directory stream */
dir = opendir (dirname);
if (dir != NULL) {
struct dirent *ent;
/* Print all files and directories within the directory */
while ((ent = readdir (dir)) != NULL) {
char *q = p;
char c;
/* Get final character of directory name */
if (buffer < q)
{
c = q[-1];
} else {
c = ':';
}
/* Append directory separator if not already there */
if (c != ':' && c != '/' && c != '\\')
{
*q++ = '/';
}
/* Append file name */
src = ent->d_name;
while (q < end && *src != '\0') {
*q++ = *src++;
}
*q = '\0';
/* Decide what to do with the directory entry */
switch (ent->d_type) {
case DT_LNK:
case DT_REG:
/* Output file name with directory */
printf ("%s\n", buffer);
break;
case DT_DIR:
/* Scan sub-directory recursively */
if (strcmp (ent->d_name, ".") != 0
&& strcmp (ent->d_name, "..") != 0) {
find_directory (buffer);
}
break;
default:
/* Ignore device entries */
/*NOP*/;
}
}
closedir (dir);
ok = 1;
} else {
/* Could not open directory */
printf ("Cannot open directory %s\n", dirname);
ok = 0;
}
return ok;
}
After playing with the code for awhile and understanding how it worked I did away with the while loop at line 45 because it is rare that I use command line arguments. After that I did away with the if statement at line 56 and just kept line 58. To that I added a variable for the path and a vector to hold the file names.
Line 58 in the function call parameter is where you will put the path that you want to list.
he case case DT_DIR: is a recursive call, but I see no way to break out of the recursion. A good start, but I think it needs some work.
It seems to me OP is asking how to pass command line parameters without using the console.
he case case DT_DIR: is a recursive call, but I see no way to break out of the recursion.
A directory with no subdirectories causes the control flow to never enter that switch case for that directory. Since a directory tree can contain no cycles (other than links, which are not followed here), it's not possible for this function to recurse infinitely.