如何使用C ++获取硬盘上文件和目录的列表? [英] How can i get the listing of files and directories on hard disk using c++?
问题描述
大家好!
我正在使用目录类来获取此信息,但无法将此数据分配给我自己的类的数据成员(我在做oop项目).
此外,我想使用动态性(包含)的概念.
我在下面创建了两个类,mydirectory和myfiles:
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;
}
如果要使用语句: Directory::GetDirectories("D:\\");
,如何将目录名称分配给目录类的"dname"?
我不想包含第三方软件.
我还需要以下主题的帮助:如何从控制台外部的C ++代码打开文件(doc文件/pdf/txt/pwt等)?
我很担心.
请帮我.
提前谢谢.
(我是C ++的新手,所以在指针处理中有任何错误,请原谅我,因为这是我第一次这样做.我还需要一些阅读材料.)
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.)
推荐答案
签出MSDN中的FindFirstFile()
和FindNextFile()
.
要打开文件,可以使用ShellExecute()
.这将打开所有具有识别/关联扩展名的文件.
Check out MSDN forFindFirstFile()
andFindNextFile()
.
To open a file, you can useShellExecute()
. This will open any file that has a recognized/associated extension.
目录:: GetDirectories("D:\\")
Directory::GetDirectories("D:\\")
此方法用于托管代码.如果您确实在使用CLI/C ++,请查看文档提供的代码示例: DirectoryInfo.GetDirectories [^ ].
另一方面,如果您正在编写本机代码,则按照Hans的建议,签出 ^ ], FindNextFile [
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.
沉迷于其他解决方案,请考虑对类进行更好的设计,否则您的代码将变得一团糟.
a)有时使用string
(您的意思是std :: string ??)有时是字符数组,有时是char *.要保持一致!
b)目录应形成层次结构.如何只用一个指针就能做到呢?
c)您已经错误地混合了类和数组:string fname[25]
是25个string
-s的数组.fname=NULL
没有意义(并可能导致灾难)
d)25
幻数从哪里来?
e)file
和directory
是完全无关的野兽,还是应该有共同的祖先(某种filesytementity
)来管理名称和连词?
考虑一下:
In addiction to other solution, consider also a better design of your classes, or your code will became a mess.
a) You use sometimesstring
(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 25string
-s.fname=NULL
is meaningless (and can lead to disasters)
d) Where does the25
magic number comes from ?
e) Arefile
anddirectory
completely unrelated beasts or should they have a common ancestor (a sort offilesytementity
) managing names and conjunctions?
consider this:
#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.
这篇关于如何使用C ++获取硬盘上文件和目录的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!