ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Really it's that simple of a question. How do I get an array filled with the complete path to a group of files. I could use the find > files, but I would rather allow the program to see what files are avalable. I haven't ran across any command with istream or ostream that helps with this. Any ideas?
Yes, that's correct. Do you know what I can use to get that in C++. I actually want to rename the files based on the directory name before them. So, I need a way to rename them to.
Last edited by binarybob0001; 10-23-2007 at 03:51 AM.
Thank you so much for the code! There is a problem though. I need to know if what's listed is a directory or a file. Is there a way to make it differentiate between these or do I just have to try and open up everything in the listing as a directory and handle it as an error. I know it sounds like I'm recreating find command, but I'm not. I have a large collection of files that I catagorized into folders and I don't want them in folders anymore. So I'm appending the name of the previous directory to file names in the folders. I need to be careful though and that's why I'm writing this.
Just wanted to say thankyou. I currently have a really good start on the program I wanted to make. Should be done by tommorrow. I'll post it here just for fun. There probably is a way a script can accomplish what this will, but I suck at writing scripts. I do know C++ fairly well though.
I succeeded in finally make my program. I wanted to share a class I made from it. I admit the programming is only B quality. It is not exception safe and could use a lot more functions and improvement, but here it is in case you find yourself in the need to search for files.
Code:
#include <iostream>
#include <cstdlib>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <vector>
#include <stdio.h>
using namespace std;
class directory
{
string startPath; //the directory currently being looked at
string entry; //contains the current file in the directory
string path; //is the directory + entry and has "/" appended if entry is a directory
DIR* dirHandle;
public:
directory( string d = "./") {
startPath = d;
dirHandle = NULL;
}
~directory() {
if ( dirHandle != NULL )
close();
}
bool open() {
dirHandle = opendir( startPath.c_str() );
if ( !dirHandle )
return false;
return true;
}
bool close() {
if ( closedir(dirHandle) == -1 )
return false;
dirHandle = NULL;
return true;
}
bool nextFile() {
struct dirent* e = readdir(dirHandle);
if (!e)
return false;
entry = e->d_name;
path = startPath + entry;
if (dir() )
path += "/";
return true;
}
string getPath() const {
return path;
}
string getFileName() const {
return entry;
}
string getStartPath() const {
return startPath;
}
bool dir() {
struct stat buf;
stat( path.c_str(), &buf);
return (buf.st_mode & S_IFMT) == S_IFDIR;
}
bool file() {
struct stat buf;
stat( path.c_str(), &buf);
return (buf.st_mode & S_IFMT) == S_IFREG;
}
bool hidden() {
if ( entry[0] == '.' )
return true;
return false;
}
void rewind() {
rewinddir(dirHandle);
}
void list() {
string backup = entry;
long int pos = telldir(dirHandle);
rewinddir(dirHandle);
while ( nextFile() )
cout << getFileName() << endl;
seekdir(dirHandle, pos);
entry = backup;
}
};
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.