Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old May 16th, 2008, 7:29 AM   #1
Marijan
Newbie
 
Join Date: Feb 2008
Posts: 15
Rep Power: 0 Marijan is on a distinguished road
File comparison program

The end of my first school year as a programmer is closing in.We heave learned the basic things of C++ programming.I have been doing quite good in programming so far and i passed my exam with 9 grade(9/10).But the real chalenge for me came today.I have accepted to write a program which will help me pass the 2nd exam.I am not quite sure if i can do it all alone,but it's ok since i am allowed to use all the sources avaliable to me.So far this community has been really helpfull for me and i hope it will stay that way.
The assistant of my programming professor came with a smart idea that i should make a program that will compare files in 1 folder and all of it's subfolders.It should compare their names,their size,if they are hidden and system files.And if these conditions are true it should print the names of those files and their locations.
I want you to know that my primary objective is to learn how to program and i,by any means don't want anyone to write this thing for me,i only need some kind of starting tips and pointers and some usefull thing that will help me writing my program.
Thanks in advance.
Marijan is offline   Reply With Quote
Old May 16th, 2008, 8:15 AM   #2
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 545
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: File comparison program

You need to call a system function to get a list of all the files in a folder. Then use recurson to process all its subfolders, and subfolders of the subfolders.

_findfirst() and _findnext() are useful functions to do that. google for those functions and you will find lots of examples. Here is just one of many.
__________________
True Terror is to wake up one morning and discover that your high school class is running the country - Kurt Vonnegut Jr.
Ancient Dragon is offline   Reply With Quote
Old May 16th, 2008, 1:20 PM   #3
Sorrofix
Expert Bug Developer
 
Sorrofix's Avatar
 
Join Date: Apr 2008
Posts: 21
Rep Power: 0 Sorrofix is on a distinguished road
Re: File comparison program

Which operating system do you intend to write this program for?
Sorrofix is offline   Reply With Quote
Old May 16th, 2008, 2:13 PM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Re: File comparison program

Quote:
Originally Posted by Marijan View Post
The assistant of my programming professor came with a smart idea that i should make a program that will compare files in 1 folder and all of it's subfolders.It should compare their names,their size,if they are hidden and system files.And if these conditions are true it should print the names of those files and their locations.
Firstly, you need to be aware that your program cannot be written in standard C++: the means getting names of files, their size, and attributes (system or hidden) are not part of C++. They are associated with the operating system your program will run on. Getting a list of files involves functions like findfirst()/findnext() [unix, with some portability to Windows], _findfirst(), _findnext() [typically under windows, but technically library extensions], FindFirstFile()/FindNextFile() [win 32], and other functions on other operating systems. Getting their attributes (eg are they a file or a directory, hidden/system state) also often involves functions and data structures that are operating system specific.

Second, I consider that the value in your exercise is learning to practically cope with the need to use C++ with functions/data structures/ etc that go outside scope of the C++ standard. I suggest you consider ways to structure your program to aid portability (eg have a set of functions that your code calls, which calls the right functions depending on operating system). This will be useful in terms of learning how to DETECT what operating system your code is on (eg compiler/OS specific macros), writing a program so it will compiler and then execute despite calling different functions, and use of build techniques (eg makefiles) to help with that. Keep in mind that it is usually a good idea to ensure the bulk of your code is portable (ie standard C++), so the trick is to minimise effort to achieve that. If you have more than one call of functions like findfirst() and findnext() in your code, it is not portable -- consider how you would address the problem of getting the result of functions like findfirst() to your code, without calling findfirst() on multiple places in your code because, if you call it multiple times, you need to modify that code every time you change operating system.

Actually try (i.e. have it as a specific goal) to produce a code base that will work on two different operating systems. Such as windows and a unix variant. You will encounter various portability issues, but addressing those issues will be a useful practical learning exercise for you.
__________________
Dear God
So far today I've done all right. I haven't been grumpy yet. But in a few minutes, God, I'm going to get out of bed, and from then on I'm going to need a lot more help.
AMEN
grumpy is offline   Reply With Quote
Old May 17th, 2008, 5:41 AM   #5
Marijan
Newbie
 
Join Date: Feb 2008
Posts: 15
Rep Power: 0 Marijan is on a distinguished road
Re: File comparison program

Thanks a lot,just for information i intend on making a program for windows,but just as grumpy suggested i may try to make it useable on more operating systems.
Marijan is offline   Reply With Quote
Old May 19th, 2008, 7:06 AM   #6
Marijan
Newbie
 
Join Date: Feb 2008
Posts: 15
Rep Power: 0 Marijan is on a distinguished road
Re: File comparison program

Ok, i did a research for those functions and i also found out what recursion is.But maybe i am a bit thick or i can't think straight these days because i can't find out how to use these functions properly.
When i try to use the function on some already declared string that contains the path of the file name,it makes some problems like:"Cannot convert constant char to char"
Marijan is offline   Reply With Quote
Old May 19th, 2008, 8:34 AM   #7
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 545
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: File comparison program

Please post your code so that someone can help you.
__________________
True Terror is to wake up one morning and discover that your high school class is running the country - Kurt Vonnegut Jr.
Ancient Dragon is offline   Reply With Quote
Old May 19th, 2008, 5:15 PM   #8
Marijan
Newbie
 
Join Date: Feb 2008
Posts: 15
Rep Power: 0 Marijan is on a distinguished road
Re: File comparison program

Ok this is a simple code that i made just to test this function


<c++> Syntax (Toggle Plain Text)
  1.  
  2. int main()
  3. {
  4. string path,result;
  5. path="C:\\";
  6. result=_findfirst(path)
  7. cout<<result<<endl;
  8.  
  9.  
  10.  
  11.  
  12. system("PAUSE");
  13. return 0;
  14.  
  15.  
  16. }
I wrote this without knowing what the function exactly does,just for a test as i said.
Marijan is offline   Reply With Quote
Old May 19th, 2008, 10:19 PM   #9
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 545
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: File comparison program

Quote:
Originally Posted by Marijan View Post
I wrote this without knowing what the function exactly does,just for a test as i said.
You need to learn how to look up the functions you don't understand. Just google for the function name and you will find a lot of help (usually).

Had you read this you would have found that
Quote:
If successful, _findfirst returns a unique search handle identifying the file or group of files matching the filespec specification, which can be used in a subsequent call to _findnext, or to _findclose. Otherwise, _findfirst will return –1 and set errno to one of the following values:
>>path="C:\\";
you need to add * for the search criteria. Just the beginning directory won't work
path="C:\\*.*"
__________________
True Terror is to wake up one morning and discover that your high school class is running the country - Kurt Vonnegut Jr.
Ancient Dragon is offline   Reply With Quote
Old May 20th, 2008, 2:53 PM   #10
Marijan
Newbie
 
Join Date: Feb 2008
Posts: 15
Rep Power: 0 Marijan is on a distinguished road
Re: File comparison program

Ok thanks for supporting me and helping me so far.I have used google and menaged to write something on my own using some already made programs.I came up with this code:

<c++> Syntax (Toggle Plain Text)
  1.  
  2. #include <direct.h>
  3. #include <stdio.h>
  4. #include <string>
  5. #include <iostream>
  6. #include <time.h>
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12. char path[50];
  13. struct _finddata_t cfile;
  14. intptr_t File;
  15. char buf[50];
  16. cout<<"Enter the path"<<endl;
  17. cin>>path;
  18.  
  19. _chdir(path);
  20.  
  21. File= _findfirst("*.*",&cfile);
  22.  
  23.  
  24. printf("Listing of files in the directory %s\n\n", path);
  25. printf("\nRDO HID SYS ARC FILE DATE %20c SIZE\n", ' ');
  26. printf("--- --- --- --- ---- ---- %20c ----\n", ' ');
  27. printf((cfile.attrib & _A_RDONLY) ? " Y " : " N ");
  28. printf((cfile.attrib & _A_SYSTEM) ? " Y " : " N ");
  29. printf((cfile.attrib & _A_HIDDEN) ? " Y " : " N ");
  30. printf((cfile.attrib & _A_ARCH) ? " Y " : " N ");
  31.  
  32. printf(" %-30s %.20s %9ld\n", cfile.name, buf, cfile.size);
  33.  
  34.  
  35. system("PAUSE");
  36. return EXIT_SUCCESS;
  37.  
  38. }

It does...something,it lists only files in the folder you select.But i still seem to be far from my objective.

P.S. I am a C++ "programer"(lol),so i use cout always,i copied the printf commands from another code and i tried replacing them with the cout command,but it prints out sucessfully only cfile.attrib and i don't understand the rest .
And sorry if i am being annoying.

Last edited by Marijan; May 20th, 2008 at 3:09 PM.
Marijan 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Raw C++ File Transfer Program with GUI c0ldshadow C++ 2 Aug 7th, 2006 5:04 AM
How to start a program upon file creation ? Marc_piecko C 4 May 22nd, 2006 3:52 AM
Basic File Search Program Twilight C++ 4 May 18th, 2006 3:36 PM
checking file existing time to time, in the same time program do other operation myName C++ 3 Mar 13th, 2006 8:39 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM




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

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