开发者

How can i get the listing of files and directories of a path using C++?

I am using the directory class to get this information but unable to assign this data to a data member of my own class. i am doing an oop project. Furthermore,I want to use the concept of Dynamism(containment).I have created two class, mydirectory and myfiles as under:

class files
{
    string fname[25];
public:
    files()
    {
      fname=NULL;
    }

};

class directory
{ private:
    directory *d;
 开发者_如何学运维   string *dname[25];   //* to show there may be a subdirectory,there may be not.
    files *ff[25];       // data member string *fname[25];
int numd,numf;
public:
    directory()
    {
    numd=0;numf=0;
    }

Now when if I want to use the statment:

 Directory::GetDirectories("D:\\"); 

how can I assign the directory names to "dname" of directory class.

I dont want to include a third party software.

also i need help on the topic: how can a file (doc file/pdf/txt/pwt etc) can be opened from c++ code outside the console? I am very worried. please help me. thanks in advance.

I am new to c++ so please forgive if there are any errors in pointer handling, as I am doing this containment for the first time. I also need some reading stuff.


The simplest way to do it in C++ is using boost::filesystem.

As long as the path is a directory you can iterate over it using either a directory_iterator or a recursive_directory_iterator.

eg:

boost::filesystem::path dirname( "D:\\" );

std::vector<boost::filesystem::path> topLevel( directory_iterator(dirName), 
      directory_iterator() );

std::vector<boost::filesystem::path> wholeDrive( 
       recursive_directory_iterator(dirName), recursive_directory_iterator() );


As this is marked homework, we're not going to be helping you much by giving you the correct answer. But I will point you in the right direction.

You've indicated you're doing this under Visual C++. Well without using any third party libraries but just what's built in, you'll need to access the Win32 API.

FindFirstFile() & FindNextFile() are what you need.

You'll call FindFirstFile first off to obtain the directory handle. The parameter is the D:\ that you're passing into your class.

Then call FindNextFile in a while loop.

e.g. The basic principle of using those API's is

HANDLE h = FindFirstFile("D:\\");
WIN32_FIND_DATA data;
while (FindNextFile(h, &data))
{
   // Check if it's a directory or not
   if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
     // Add to dname
   }
}

Consider using std::vector for your dname instead of string* because you're stuck with 25 entries. Using vector it'll grow for you.


As said CashCow, boost::filesystem

As a general rule, in C++ for such examples, you don't need any pointer. Here a some mistakes you should correct:

string fname[25];

This declares an array of 25 strings. You probably wanted a string of 25 chars ? Well, in std::string, you don't need to care about the length. std::string fname; is enough

std::string file_name;
file_name = "baz.txt";

fname=NULL;

If fname is a string, then it's not a pointer. So you can't assign NULL to it. A std::string is by default initialized as an empty string. You can leave the whole constructor out.

string *dname[25]

I suppose you wanted to have an array of string. Just use :

std::vector<std::string> dnames;
dnames.push_back("foo");
dnames.push_back("bar"); // dnames now contains {"foo","bar"}

And you'll have a dynamically resizable vector of strings.

See : no need of any pointer. No need for any new


Finally I completed the short project.To get the list of files and sub directories, I made use of .NET Framework namespace "System".It has got classes like "FileInfo" and "DirectoryInfo"(both belong to System::IO) which do the above required task.But here,all the string related stuff is of System::String , not of std::string.To convert System::String to std::string, I used the following code(I got this conversion's code from a forum and it worked fine without any error):

string Str2str(String ^a)
{
array<Byte> ^chars = System::Text::Encoding::ASCII->GetBytes(a);
pin_ptr<Byte> charsPointer = &(chars[0]);
char *nativeCharsPointer = reinterpret_cast<char *>(static_cast<unsigned char *>(charsPointer));
string native(nativeCharsPointer, chars->Length);
return native;
}

Here is a short code for getting list of sub directories from a drive(D: drive is going to be searched):

 #include<iostream>
        #using<mscorlib.dll>
        using namespace strd;        
        using namespace System;
        using namespace System::IO;


int main()
    {int count=50;
string name[count];//to hold directories names
string b;
int s=0;
DirectoryInfo^ di = gcnew DirectoryInfo("d:\\");
if(di->Exists)
{array<DirectoryInfo^>^diArr = di->GetDirectories();
Collections::IEnumerator^ myEnum = diArr->GetEnumerator();
while ( myEnum->MoveNext() )
{DirectoryInfo^ dri = safe_cast<DirectoryInfo^>(myEnum->Current);


      String ^a=(dri->Name->ToString());
      int n=b.size();
      b=Str2str(a); `// code given in the starting`
      if (s<count)
      {name[s]=b;
      s++;}

}

This involves Managed C++ knowledge. Visit these:

.NET Programming Guide

C++: The Most Powerful Language for .NET Framework Programming

I compiled this on Visual Studio 2008. I will be very grateful if you appriciate my effort.Further suggestions are most welcomed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜