Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 8th, 2008, 3:14 PM   #1
Marijan
Newbie
 
Join Date: Feb 2008
Posts: 15
Rep Power: 0 Marijan is on a distinguished road
Need some help again!

I made a topic some 2 weeks ago about a program i need to do and since then i tried hard using all the help i got and i am about to finish up my program.
My program needs to find 2 files with same name and possibly same size/creation date.The user should give the path for the desired directory and the program should solve the rest.
Now as i said i am about to finish it,since i have found solution to make the program proccess not only the desired folder but the subfolders also using recursion.Only the problem is that i am stuck a bit at the end,here is the code
i have written so far.
<c++> Syntax (Toggle Plain Text)
  1.  
  2.  
  3. #include <direct.h>
  4. #include <string>
  5. #include <iostream>
  6. #include <time.h>
  7. #include <windows.h>
  8.  
  9. using namespace std;
  10.  
  11.  
  12.  
  13. void Prebaruvanje(char *path)
  14. {
  15. WIN32_FIND_DATA data; //Win32 data structure
  16. HANDLE hFind; //the handle that is returned with the FindFirstFile func
  17. BOOL bContinue = TRUE;
  18. char NewDirPath[MAX_PATH]; //array that will store the new path
  19. char cst[3]="\\";
  20.  
  21. _chdir(path); //set the current directory to the path variable
  22.  
  23. hFind = FindFirstFile("*.*", &data);
  24.  
  25.  
  26. while (hFind && bContinue) { //while there is nothing left
  27.  
  28. if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { //directory check
  29.  
  30. if (!(strcmp(data.cFileName, ".") == 0) &&
  31. !(strcmp(data.cFileName, "..") == 0)) { //checking if the data.cFileName is a folder
  32.  
  33. cout<<data.cFileName<<endl; //if it isn't print the name of the file
  34. }else //if it is make the new path the old path+\\+the name of the folder we just found
  35.  
  36. NewDirPath=path+cst+data.cFileName; //the place where i am stuck at
  37. Prebaruvanje(szNewDirPath); //recursion
  38.  
  39. }
  40. bContinue = FindNextFile(hFind, &data);
  41.  
  42.  
  43.  
  44. }
  45.  
  46. FindClose(hFind);
  47. }
  48.  
  49. int main()
  50. {
  51. char path[50];
  52.  
  53. cout<<"Enter the path of the directory"<<endl;
  54. cin.getline(path,50);
  55.  
  56. char *pathp=path;
  57.  
  58. Prebaruvanje(pathp);
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65. system("PAUSE");
  66.  
  67. return 0;
  68.  
  69. }


Ok the code seems to be finished,exept the fact that i can't add all the elements of the 3 arrays:path cst and data cFileName in NewDirPath.If they were strings i could concatenate them,but then i couldn't use recursion on NewDirPath since the function receives pointers to array of chars only as arguments.And also as you have already noted,this function compares nothing,so far,it only prints out all the files in a directory,i would like to get an advice on that please.
Thanks in advance!

Last edited by Marijan; Jun 8th, 2008 at 3:25 PM.
Marijan is offline   Reply With Quote
Old Jun 8th, 2008, 3:36 PM   #2
Sorrofix
Expert Bug Developer
 
Sorrofix's Avatar
 
Join Date: Apr 2008
Posts: 21
Rep Power: 0 Sorrofix is on a distinguished road
Re: Need some help again!

Quote:
Originally Posted by Marijan View Post
Ok the code seems to be finished,exept the fact that i can't add all the elements of the 3 arrays:path cst and data cFileName in NewDirPath.If they were strings i could concatenate them,but then i couldn't use recursion on NewDirPath since the function receives pointers to array of chars only as arguments.
If you want to concatenate C-style strings, use strcat. Also, if you wanted to, you could use std::strings as well, you'd just have to use std::string::c_str() to convert them back when passing them to Prebaruvanje (recursion).
Sorrofix is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:21 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC