LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-23-2007, 12:00 AM   #1
binarybob0001
Member
 
Registered: Dec 2004
Distribution: Debian Wheezy
Posts: 444

Rep: Reputation: 30
Getting a File Name?


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?
 
Old 10-23-2007, 12:15 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
By your use of find > files I take it you want a listing of a directory is that correct?
 
Old 10-23-2007, 03:49 AM   #3
binarybob0001
Member
 
Registered: Dec 2004
Distribution: Debian Wheezy
Posts: 444

Original Poster
Rep: Reputation: 30
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.
 
Old 10-23-2007, 07:58 AM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
I take it you want this just for linux, yet if not just let me know.
Code:
#include <sys/types.h>
#include <dirent.h>
	std::vector<std::string> get_files(std::string const & path)
	{
		std::vector<std::string> files;
		struct dirent* entry;

		// open the directory
		DIR* dir = opendir( path.c_str() );
		if (!dir)
		{
			perror("opendir");
			exit(1);
		}

		//read the directory's contents
		while ( (entry = readdir(dir)) != NULL) 
		{
				files.push_back( entry->d_name );
		}

		//close the directory
		if (closedir(dir) == -1)
		{
			perror("closedir");
			exit(1);
		}
		return files;
	}

and   

     int rename(const char *from, const char *to) in stdio.h

Last edited by dmail; 10-23-2007 at 08:04 AM.
 
Old 10-23-2007, 05:48 PM   #5
binarybob0001
Member
 
Registered: Dec 2004
Distribution: Debian Wheezy
Posts: 444

Original Poster
Rep: Reputation: 30
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.
 
Old 10-23-2007, 06:07 PM   #6
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Check out stat and its mode_t member seeing if it has the S_IFDIR bits set.
http://www.opengroup.org/onlinepubs/...ys/stat.h.html
I would have thought a script would be better for this though.

Last edited by dmail; 10-23-2007 at 06:08 PM.
 
Old 10-24-2007, 04:42 AM   #7
binarybob0001
Member
 
Registered: Dec 2004
Distribution: Debian Wheezy
Posts: 444

Original Poster
Rep: Reputation: 30
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.
 
Old 10-31-2007, 05:51 AM   #8
binarybob0001
Member
 
Registered: Dec 2004
Distribution: Debian Wheezy
Posts: 444

Original Poster
Rep: Reputation: 30
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;
  }
};
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
wxPython problem: /_core_.so: cannot open shared object file: No such file or directo aregmi Linux - Software 2 06-06-2007 08:53 PM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM
file /usr/bin/mysqlbug from install of mysql-3.23.58-1.9 conflicts with file from pac commandline Linux - Software 2 09-28-2006 05:55 PM
gave wrong syntax for tar as tar -cvzf file file.tgz how to recover the file gautham Linux - General 4 04-13-2005 03:15 AM
How to play a media file/ video file/mp3 file recorded in harddisk/cd-rom arindam Linux - Newbie 2 09-05-2003 10:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration