I need to write a C program for a friend that counts the number of dat files in a certain directory, however, I don't know how to count files in C. Can anyone help me out?
I need to write a C program for a friend that counts the number of dat files in a certain directory, however, I don't know how to count files in C. Can anyone help me out?
Windows SDK File System: Q: How to count files within a directory and subdirectories?
Too convenient! (It's the concept that counts, not that it's C++ or not) Bad mistake of assuming Windows...I keep editing my post...but you should have posted compiler/OS in the first place.
Last edited by Tonto; 09-29-2005 at 07:24 PM.
http://www.eskimo.com/~scs/C-faq/q19.20.html
There's no truly portable way. The logic is simple, read all the files in the directory, check if they end in .dat, increment a counter.
For reading files in a directory, there's opendir/readdir in POSIX environments.
ls *.dat | wc
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Salem,
That's not a C program
I think you meant
Code:#include <stdlib.h> int main(void) { return system("ls *.dat | wc"); }
Code:#include <iostream> #include <windows.h> #include <string> #include <fstream> using namespace std; int main() { fstream ft; ft.open("store.txt",ios::out); WIN32_FIND_DATA FindFileData; HANDLE hFind = INVALID_HANDLE_VALUE; char DirSpec[MAX_PATH]; // directory specification cout<<"Please enter your path"<<endl; cout<<"\nEg. C:\\Program Files\\Common Files\\System "<<endl; cout<<">>"; cin.get(DirSpec, MAX_PATH); cout<<"\n"; strncat(DirSpec, "\\*", 3); hFind = FindFirstFile(DirSpec, &FindFileData); int cond=0; if(hFind == INVALID_HANDLE_VALUE) { cout<<"Error: invalid path\n"; cond=1; } if (cond!=1) { while(FindNextFile(hFind, &FindFileData) != 0) { char array[81]; ft<<FindFileData.cFileName<<"\n"; cout<<FindFileData.cFileName<<"\n"; } FindClose(hFind); } ft.close(); /*------------------------------------------------------*/ cout<<"\n\nNow enter the extension of the files you wish to count"<<endl; cout<<"Example (mp3 or dat...)"<<endl; cout<<">>"; char extension[81]; cin>>extension; int s_e = strlen(extension); fstream file_pointer; file_pointer.open("store.txt",ios::in); int file_count=-1; //to account for eof anomoly do { char stuff[100]; file_pointer>>stuff; int counter=0; int check=0; int s = strlen(stuff); for (int i=0; i<s; i++) { if(stuff[i]==extension[0]) { for (int b=0; b<s_e; b++) { if(stuff[i+b]==extension[counter]) { check++; counter++; } } if(check==s_e) { file_count++; } counter=0; check=0; } } }while(file_pointer.peek()!=EOF); file_pointer.close(); cout<<"\nThere were "<<file_count<<" files found"; cin.get(); cin.get(); }
My sample input...
This code was written using a snippet from the FAQ. It's probably a lot more cumbersome than it should be but it appears to work. Basically i'm doing what cwr suggested.Code:Please enter your path Eg. C:\Program Files\Common Files\System >>C:\program files\common files\system .. ado directdb.dll msadc Ole DB wab32.dll wab32res.dll Now enter the extension of the files you wish to count Example (mp3 or dat...) >>.dll There were 3 files found
treenef: your program will miss the first file because you toss its filename into the bit bucket. FindFirstFile() returns the first filename in the WIN32_FIND_DATA and you need to add it to the list before calling FindNextFile() the first time. use a do-while loop makes this easier to code.
Code:do { ft<<FindFileData.cFileName<<"\n"; cout<<FindFileData.cFileName<<"\n"; } while( FindNextFile(hFind, &FindFileData) != 0);
anicent: You're right. I didn't know of this since I just cut and pasted the code from a snippet i found in the FAQ.
Also I've noticed there is a problem with EOF as mentioned in the FAQ which causing other file counting problems. Hopefully, here is a corrected version...
Code:#include <iostream> #include <windows.h> #include <string> #include <fstream> using namespace std; int main() { fstream ft; ft.open("store.txt",ios::out); WIN32_FIND_DATA FindFileData; HANDLE hFind = INVALID_HANDLE_VALUE; char DirSpec[MAX_PATH]; // directory specification cout<<"Please enter your path"<<endl; cout<<"\nEg. C:\\Program Files\\Common Files\\System "<<endl; cout<<">>"; cin.get(DirSpec, MAX_PATH); cout<<"\n"; strncat(DirSpec, "\\*", 3); hFind = FindFirstFile(DirSpec, &FindFileData); int cond=0; if(hFind == INVALID_HANDLE_VALUE) { cout<<"Error: invalid path\n"; cond=1; } if (cond!=1) { do { ft<<FindFileData.cFileName<<"\n"; cout<<FindFileData.cFileName<<"\n"; } while( FindNextFile(hFind, &FindFileData) != 0); FindClose(hFind); } ft.close(); /*------------------------------------------------------*/ cout<<"\n\nNow enter the extension of the files you wish to count"<<endl; cout<<"Example (mp3 or dat...)"<<endl; cout<<">>"; char extension[81]; cin>>extension; int s_e = strlen(extension); char stuff[81]; int file_count=0; ifstream file_pointer("store.txt"); while(file_pointer>>stuff) { int counter=0; int check=0; int s = strlen(stuff); for (int i=0; i<s; i++) { if(stuff[i]==extension[0]) { for (int b=0; b<s_e; b++) { if(stuff[i+b]==extension[counter]) { check++; counter++; } } if(check==s_e) { file_count++; } counter=0; check=0; } } } file_pointer.close(); cout<<"\nThere were "<<file_count<<" files found"; cin.get(); cin.get(); }
Last edited by treenef; 09-30-2005 at 03:29 PM.
You could actually do the program without using the file. Get the extension from the keyboard first then search the directory for it. That eliminates the need for the file.
I appreciate all the help. Thanks!