|
hi All,
I'm looking for an article and/or source code for a super fast search algorithm. I'm traversing a directory searching for certain files, but there is a huge amount of files involved and I need to group the files together. I need to be able to do this preferably using a super fast search method.
Any help is greatly appreciated.
thank you.
|
|
|
|
|
So you're only searching for specific file names?
There are two ways that come to mind:
1. Use the FindFirstFile and FindNextFile APIs. You can also go with the CFileFind class, which is probably only a little less efficient than calling the APIs directly. As you read the file names, organize them into groups.
2. Do a "DIR" into a file:
WinExec("dir /b > zzFiles.txt", SW_HIDE);
Read each line of zzFiles.txt up to the last one (which should be zzFiles.txt) and get the file names from there. I haven't timed it, but I bet the first approach is more efficient.
Regards,
Alvaro
Well done is better than well said. -- Benjamin Franklin
(I actually prefer medium-well.)
|
|
|
|
|
Right, That's actually what I am doing right now is using the FindFirstFile and FindNextFile. So I'm not really searching for 1 specfic filename. I'm writing a custom "windows explorer" which requires me to take a series of files in a directory and group them togther, display the master file.
So at this time, I'm searching through the directory several times:
- once to find the master files
- then one time for every master file I have found
So, there could be 5,000 files in the directory with say, 50 master files. This would result in traversing the directory 50+1 times, totalling 51 independent searches in 1 directory.
Becuase of this, I am looking to figure out a method that requires searching the directory only once, and group the files as I go using an container class or something. But I don't know enough about STL yet and it would take too long to write 1 up on my own.
Sorry I wasn't this clear the first time. but that is my real delimea. What I want to do is use STL in such a way that I could just traverse the directory once, creating a map such as:
master_file1: file1, file2, file3
master_file2: file1, file2, file3
Any ideas? Anyone? Thanks alot!
|
|
|
|
|
OK, so you need a map of vectors:
1. Define your types
using namespace std;
typedef string FileName;
typedef vector< FileName > FileNames;
typedef auto_ptr< FileNames > FileNamesPtr;
typedef map< FileName, FileNamesPtr > MasterToFileNamesPtrMap;
2. Then you can use:
MasterToFileNamesPtrMap map;
FileNamesPtr pFN(new FileNames);
pFN->push_back(fileName);
map[masterFileName] = pFN;
Regards,
Alvaro
Well done is better than well said. -- Benjamin Franklin
(I actually prefer medium-well.)
|
|
|
|
|
Isn't that Simple.... Thanks. I've been reading up on STL. I think I'm gonna start using it. I have lots of uses for it. Just have to learn more.
Thank you very much for your help. The example you gave helps alot in explaining a solution to my problem, but more importantly helps me understand STL much more as it is applied to one of my specific applications.
Thank you. I appreciate it greatly Alvaro.
|
|
|
|
|
typedef std::list<std::string> FileList;
typedef FileList::iterator FileListIterator;
struct SortByDate : public std::binary_function<std::string, std::string, bool>
{
public:
SortByDate(const char* dir) : m_dir(dir) {}
bool operator()(const std::string& lhs, const std::string& rhs)
{
struct stat st = {0};
std::string filename = m_dir;
filename += "\\";
filename += lhs;
if (stat(filename.c_str(), &st) == -1)
{
return true;
}
int lhs_ctime = st.st_ctime;
filename = m_dir;
filename += "\\";
filename += rhs;
if (stat(filename.c_str(), &st) == -1)
{
return false;
}
int rhs_ctime = st.st_ctime;
return lhs_ctime < rhs_ctime;
}
private:
const char* m_dir;
};
bool GetDirectoryList(const char* dir, FileList& fileList)
{
DEBUGF("GetDirectoryList %s", dir);
HANDLE hSearch;
WIN32_FIND_DATA FindData;
char tmpDir[MAX_PATH*2];
lstrcpy(tmpDir, dir);
lstrcat(tmpDir, "\\*.*");
hSearch = FindFirstFile(tmpDir, &FindData);
if (INVALID_HANDLE_VALUE == hSearch)
{
return false;
}
do
{
if (!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
fileList.insert(std::upper_bound(fileList.begin(),
fileList.end(),
FindData.cFileName,
SortByDate(dir)),
FindData.cFileName);
}
}
while (FindNextFile(hSearch, &FindData));
FindClose(hSearch);
return true;
}
Here's a simple example. You can exclude the SortByDate function if you like. I should have made it more STL like but didn't have time.
Todd Smith
|
|
|
|
|
Hi guys
Do tyou know the way that when CFileDialog is called it goes to the last visited directory (or last directory shown when CFileDialog was called last time).
Best regards and Mery Christmas.
Dr. PI
|
|
|
|
|
The way I do it, is to save the path for the last file my application opened in the registry. Then the next time I need to load the file dialog, I extract the path from the registry and use it to set the dialog's m_ofn.lpstrInitialDir...before I call DoModal
Gary Kirkham
A working Program is one that has only unobserved bugs
|
|
|
|
|
Gary Kirkham wrote:
A working Program is one that has only unobserved bugs
You are the only person I've found online who echoes this sentiment which is near and dear to my heart. My CP sig used to be the same as my email sig, which is "There is always one more bug...".
/ravi
Let's put "civil" back in "civilization"
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
Another one in that vein:
Every computer program has at least one bug and can be shortened by one instruction...from which, by induction, one can assert that every program can be reduced to one instruction which doesn't work.
Gary Kirkham
A working Program is one that has only unobserved bugs
|
|
|
|
|
Hmm, that would imply a program with zero instructions is buggy.
/ravi
Let's put "civil" back in "civilization"
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
Hi -
I have a question which is probably very simple...
I'm trying to build an application which requires an open-file dialog like the explorer in windows (or like any other app that works with files). I want to use Windows Forms in Visual C++.net.
Does anyone have some canned code that generates an open-file dialog with a TreeView to the left and a ListView to the right, that allows the user to select a file from the ListView by clicking on the appropriate icon?
I would be very, very grateful!
Thanks,
KBL
|
|
|
|
|
When should I go for a win32 application instead of an MFC application?
Where could I find a good online tutorial about win32 programming? And MFC?
Thanks so much!
-- naradaji
|
|
|
|
|
MFC is like the Devil and plain Win32 programming is like in heaven.
MFC has many memory leaks and your app will also be slower with MFC than a plain written Win32 app.
Learn plain Win32 programming first if you are new on this topic!
Then when you know Win32 API programming you can learn MFC if you want... but start with Win32 API!!
Rickard Andersson@Suza Computing
C# and C++ programmer from SWEDEN!
UIN: 50302279
E-Mail: nikado@pc.nu
Speciality: I love C#, ASP.NET and C++!
|
|
|
|
|
Rickard Andersson wrote:
MFC is like the Devil and plain Win32 programming is like in heaven.
Not that I am a fan of MFC, but "plain" Win32 programming is very tedious, error prone and unproductive. If you don't like MFC, go to ATL window classes, or WTL. They are much easier and safer to work with, and impose no overhead.
Rickard Andersson wrote:
MFC has many memory leaks
Would you mind to show an example?
|
|
|
|
|
Nemanja Trifunovic wrote:
Would you mind to show an example?
Memory leaks in MFC are why we did not buy boundschecker. It's too much work trying to only see the leaks that are your fault, and too hard to find them under all the ones in MFC. I suspect a lot of them are percieved leaks due to badly written code, but if you get a demo and run an MFC app, you'll soon see what I mean.
Christian
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002
|
|
|
|
|
You can take ICQ as an example!
It is written in MFC and eat your memory like a horse eat usual hores food!!
Rickard Andersson@Suza Computing
C# and C++ programmer from SWEDEN!
UIN: 50302279
E-Mail: nikado@pc.nu
Speciality: I love C#, ASP.NET and C++!
|
|
|
|
|
MFC is a bunch of macros, global functions, and C++ classes, most of which wrap around the Win32 API. So if you learn the Win32 API, you should have little trouble picking up MFC. However, if you already know and like C++, then don't bother with Win32 -- you'll learn it on the way -- start with MFC. It's a lot more fun and easy to work with.
I don't know about online tutorials but I learned MFC with one of the tutorials that came with the Visual C++ help (back in the day). The name of that tutorial is "Scribble". Look it up in the online help. It was great for learning a lot of different aspects of Windows development with MFC.
Regards,
Alvaro
Well done is better than well said. -- Benjamin Franklin
(I actually prefer medium-well.)
|
|
|
|
|
I use the book "Programming Windows with MFC" by Jeff Prosise. It is very helpful. Not absolutely the bible for MFC, but it gives an excellent play by play and the examples included with the book have given me a huge boost on the learning curve for MFC. It also has an online version with it, so I was able to put it on my laptop and take it everywhere with me.
I did know Win32 and that helped. Between this book, Code Project, and the forum here, I have had much success in using MFC and am increasing my knowledge base everyday.
Good luck!
|
|
|
|
|
Win32 is much more tedious and error prone than MFC, and even MFC is pretty old technology. I'd learn MFC and then drill down and learn Win32 later. I'd never use Win32 for anything unless I had very good reason, but it's good to learn it after a while so you understand what MFC is doing for you.
Christian
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002
|
|
|
|
|
I strongly recommend using MFC over the Win32 API for any real-world application. However, I also strongly recommend knowing the Win32 API, since MFC is but a thin wrapper over this.
/ravi
Let's put "civil" back in "civilization"
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
I just wanna thank you all for your answers, however, so far nobody has mentioned any online tutorial on MFC and / or Win32. Any ideas?
Thanks again!
-- naradaji
|
|
|
|
|
Another idea:
could anybody give me examples on commercial MFC applications? And Win32?
And if anybody knows about on line tutorials....
Thanks!
|
|
|
|
|
|
Maybe I didn't make myself clear. Of course I know how to go to google and type 'MFC Tutorial'. I've done it hundreds of time, only about MFC and VC++. What I meant was rather something like: have you found ANY tutorial on the web that you consider good and useful. Can you PERSONALLY recommend me / direct me to one in particular?
I'm telling you: even though when you type 'MFC Tutorial' in google, you get a lot of links, none will take you to a decent TUTORIAL (maybe one, that is: I've only found one). Most of them are an introduction to MFC programming. Thet usually deal with the Visual Studio IDE, the wizards, the 'Hellow World!' application and little more. A tutorial is something more, mind you.
The one I found was by Kate Gregory about VC++ 6, and it's pretty complete (though not advenced at all). It's the only decent one I've found.
Has anybody found any other?
Thanks for all your replies!
-- naradaji
|
|
|
|