🔒 Locked
Sourcecode lines counter utility
Advertisement
Original Post
Thanks.
That's not what I asked for, though.
maybe my next question should be(seriously):
How do open up the directory for reading without knowing how many files are available to be read in that directory or what the names of the files are? Once I'm in the file I'm OK (I think)- I just don't know how to query for a file where I don't know it's name.
Once in the file, I what, search for text the preceeds CRLFs and count the number of CRLFs - ignoring lines that contain no text?
Oh, I'm on a winbox, btw.
[edited by - Like2Byte on June 12, 2002 10:24:42 AM]
That's not what I asked for, though.
maybe my next question should be(seriously):
How do open up the directory for reading without knowing how many files are available to be read in that directory or what the names of the files are? Once I'm in the file I'm OK (I think)- I just don't know how to query for a file where I don't know it's name.
Once in the file, I what, search for text the preceeds CRLFs and count the number of CRLFs - ignoring lines that contain no text?
Oh, I'm on a winbox, btw.
[edited by - Like2Byte on June 12, 2002 10:24:42 AM]
If you are using VS6, then Project Line Counter is free and integrates with the IDE.
Look here for information on using the FindFirstFile, FindNextFile and FindClose API functions.
Here is the code (I haven''t tested it, you would likely be best to read the docs in that link)
Note: If I remember correctly FindFileData.cFileName gives only the filename, not the complete path ("myfile.cpp" rather than "c:\documents\myfile.cpp")
Here is the code (I haven''t tested it, you would likely be best to read the docs in that link)
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
hFind = FindFirstFile("c:\documents\*.cpp", &FindFileData);
if (hFind != INVALID_HANDLE_VALUE)
{
CountLinesInFileCalled(FindFileData.cFileName);
while (FindNext(hFind, FindFileData))
{
CountLinesInFileCalled(FindFileData.cFileName);
}
FindClose(hFind);
}
Note: If I remember correctly FindFileData.cFileName gives only the filename, not the complete path ("myfile.cpp" rather than "c:\documents\myfile.cpp")
Heres a small program I wrote with that method, if you want to see how it works when put together.
Sorry about the comments,
Ballistic Programs
[edited by - Drazgal on June 12, 2002 12:37:46 PM]
#include <fstream.h>
#include <windows.h>
#include <io.h>
#include <string.h>
void Count(char FullPath[561],int& Total,char FilePath[260]);
int main()
{
char PathName[300];
char FullPath[561];
int Counter = 0;
int Total = 0;
cin >> PathName;
SetCurrentDirectory(PathName); // Set directory to working one
struct _finddata_t c_file; // Struct to hold data
long hFile; // File handle
if( (hFile = _findfirst( "*.cpp", &c_file )) == -1L ) // Search for a file
{} // If not file found
else
{
strcpy(FullPath,PathName);
strcat(FullPath,"\\");
strcat(FullPath,c_file.name);
Count(FullPath,Total,c_file.name);
while( _findnext( hFile, &c_file ) == 0 ) // keep going til no files found
{
strcpy(FullPath,PathName);
strcat(FullPath,"\\");
strcat(FullPath,c_file.name);
Count(FullPath,Total,c_file.name);
}
_findclose( hFile ); // Close file
}
if( (hFile = _findfirst( "*.h", &c_file )) == -1L ) // Search for a file
{} // If not file found
else
{
strcpy(FullPath,PathName);
strcat(FullPath,"\\");
strcat(FullPath,c_file.name);
Count(FullPath,Total,c_file.name);
while( _findnext( hFile, &c_file ) == 0 ) // keep going til no files found
{
strcpy(FullPath,PathName);
strcat(FullPath,"\\");
strcat(FullPath,c_file.name);
Count(FullPath,Total,c_file.name);
}
_findclose( hFile ); // Close file
}
cout << Total << " Lines in total\n";
return 0;
}
void Count(char FullPath[561], int& Total,char FilePath[260])
{
int Counter = 0;
ifstream fin(FullPath);
char ch;
while(fin.get(ch))
{
if(ch == '\n')
Counter++;
}
Counter++;
fin.close();
cout << FilePath << " has " << Counter << " Lines\n";
Total += Counter;
}
Sorry about the comments,
Ballistic Programs
[edited by - Drazgal on June 12, 2002 12:37:46 PM]
Can someone show me this for VB? The problem with VB is the multiple different types of files, the various attributes stored in them(open em in notepad), etc.
Sure I could write a counter myself, but I''ve got better things to do.
____________________________________________________________
Direct3D vs. OpenGL
Sure I could write a counter myself, but I''ve got better things to do.
____________________________________________________________
Direct3D vs. OpenGL
Hey, hats off to everyone that contributed.
I''ve read portions of these two books, Programming Windows( 5th edition) and Programming Windows with MFC(2nd edition), about the windows API. I don''t recall seeing "FindFirstFile, FindNextFile and FindClose" so I''m pretty far from being an expert on the windows API.
Hmmm, let me check...Ahh, here they are! ( And for once, they''re aptly named!!
)
Thanks again.
I''ve read portions of these two books, Programming Windows( 5th edition) and Programming Windows with MFC(2nd edition), about the windows API. I don''t recall seeing "FindFirstFile, FindNextFile and FindClose" so I''m pretty far from being an expert on the windows API.
Hmmm, let me check...Ahh, here they are! ( And for once, they''re aptly named!!
Thanks again.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement