Click here to Skip to main content
16,004,227 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi everyone!

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;
	}


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 me if there are any errors in pointer handling, as I am doing this containment for the first time. I also need some reading stuff.)
Posted
Updated 13-Mar-11 22:21pm
v3
Comments
Dalek Dave 14-Mar-11 4:22am    
Edited for Grammar and Readability.
WajihaAhmed 14-Mar-11 10:41am    
Thanks alot for doing the favour:)

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:
http://msdn.microsoft.com/en-us/library/68td296t.aspx

C++: The Most Powerful Language for .NET Framework Programming:
http://msdn.microsoft.com/en-us/library/ms379617%28VS.80%29.aspx

I compiled this in Visual Studio 2008.Further suggestions are most welcomed.
 
Share this answer
 
v2
In addiction to other solution, consider also a better design of your classes, or your code will became a mess.

a) You use sometimes string (did you mean std::string ??) sometime character arrays, sometime char*. Be consistent!
b) Directories should form a hierarchy. How can you do it with just one pointer?
c) You already mix-up classes and arrays improperly: string fname[25] is a array of 25 string-s. fname=NULL is meaningless (and can lead to disasters)
d) Where does the 25 magic number comes from ?
e) Are file and directory completely unrelated beasts or should they have a common ancestor (a sort of filesytementity) managing names and conjunctions?

consider this:
C++
#include <string>
#include <list>
#include <iostream>
class file;
class directory;
class filesystementity
{
    friend class directory;
private:
    std::string name; //the short name for a file or a dir
    directory* owner; //the owner of this entity
public:
    virtual ~filesystementity() {}
    virtual void set_name(const std::string s); //name=s, after validating the characters
    virtual std::string get_name() const; //return name (no pathname)
    virtual std::string get_pathname() const; //return C:\a\b\c\d.ext
    virtual directory* get_owner() const; //returns
};
class directory: public filesystementity
{
private:
    typedef std::list<filesystementity*> entitylist;
    entitylist entries; //files and subdirs
public:
    ~directory(); //delete all entries
    directory(const std::string& path); //create entries based on "path" content
    // iterate the children
    typedef entitylist::iterator iterator;
    iterator begin() { return entries.begin(); }
    iterator end() { return entries.end(); }
    // gets the subtypes
    static directory* is_dir(iterator i) { return dynamic_cast<directory*>(*i); }
    static file* is_file(iterator i) { return dynamic_cast<file*>(*i); }
};
class file: public filesystementity
{
public:
    //open, colose, read write ...
};


Dynamic memory is used only for polymorphic objects, not for string related stuffs.
 
Share this answer
 
v2
Comments
WajihaAhmed 14-Mar-11 10:49am    
You made my day!THanks alot for the design.
Sergey Alexandrovich Kryukov 14-Mar-11 13:21pm    
Good points, effort,
--SA
programming nerd wrote
Directory::GetDirectories("D:\\")
This method is for managed code. If you are indeed using CLI/C++ the have a look at the documentation provided code sample: DirectoryInfo.GetDirectories[^].

On the other hand, if you're writing native code then check out, as Hans already suggested, FindFirstFile[^], FindNextFile[^] documentation.
 
Share this answer
 
Check out MSDN for FindFirstFile() and FindNextFile().

To open a file, you can use ShellExecute(). This will open any file that has a recognized/associated extension.
 
Share this answer
 
v2
Comments
Dalek Dave 14-Mar-11 4:22am    
Good Call, Hans.
WajihaAhmed 14-Mar-11 10:44am    
Thanks for directing towards ShellExecute!
Sergey Alexandrovich Kryukov 14-Mar-11 13:20pm    
This is the answer, a 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900