Implementing a file counter (like "ls | wc")
Quoted 2 lines · On Wed, 07 Apr 2004 11:27:02 -0300 Luciano Moreira - igLnx…
We implemented a program that use "
opendir()", "readdir()", and "closedir()" to scan a directory and count the files contained into it.
How U use this functions?
But I'm think that it's not good method
Quoted 8 lines
But, it program have taking more time than "ls | wc" to count files.
Does have any other way to count files faster?
PS: Our program needs sometime to filter files by extension (like: *.c, *.cpp, and so on. Thus we use
strcmp()to compare last letters of both strings).
If U want a more speed don't use strcmp or strncmp functions.
Quoted 3 lines
Luciano
U use a C++ ? or C ?
Quoted 4 lines
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
---===---
Alphex Kaanoken
Senior developer of Crew IT research labs
web: http://crew.org.ru
mailto:[email protected]
---===---
Alphex Kaanoken
Senior developer of Crew IT research labs
web: http://crew.org.ru
mailto:[email protected]
-------------- THE CODE HAVE THIS STRUCTURE:
while ((pFile=readdir(pDir))!=NULL) {
sprintf(szBuf, "%s/%s", pPath, pFile->d_name);
stat(szBuf, &statFile);
if (S_ISDIR(statFile.st_mode)) /// LOOK THAT: We don't use
recursive searching, we count only files at current directory excluding others directories.
continue;
/* Filtering */
if (nNeedFilter) {
//// I DONT HAVE THE CODE OF FILTERING NOW
//// BUT I CAN SEND IT LATER IF NECESSARY
} }
-------------- CODE FINISH HERE
Does have another mechanism to filter without using of strcmp() / memcmp() ? How if we dont know the size of extension (.c, .cpp, .teste, .longextension, and so on). ?
Luciano
Quoted 53 lines · Alphex K.
Quoted 3 lines · On Wed, 07 Apr 2004 11:27:02 -0300 Luciano Moreira - igLnx…
We implemented a program that use "
opendir()", "readdir()", and "closedir()" to scan a directory and count the files contained into it.How U use this functions?
But I'm think that it's not good methodQuoted 7 lines
But, it program have taking more time than "ls | wc" to count files.
Does have any other way to count files faster?
PS: Our program needs sometime to filter files by extension (like: *.c, *.cpp, and so on. Thus we use
strcmp()to compare last letters of both strings).If U want a more speed don't use strcmp or strncmp functions.
Quoted 4 lines
Luciano
U use a C++ ? or C ?
Quoted 6 lines
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html---===---
Alphex Kaanoken
Senior developer of Crew IT research labs
web: http://crew.org.ru
mailto:[email protected]---===---
Alphex Kaanoken
Senior developer of Crew IT research labs
web: http://crew.org.ru
mailto:[email protected]
Quoted 15 lines · Luciano Moreira - igLnx
-------------- THE CODE HAVE THIS STRUCTURE:
while ((pFile=readdir(pDir))!=NULL) { sprintf(szBuf, "%s/%s", pPath, pFile->d_name); stat(szBuf, &statFile); if (S_ISDIR(statFile.st_mode)) /// LOOK THAT: We don't use recursive searching, we count only files at current directory excluding others directories. continue; /* Filtering */ if (nNeedFilter) { //// I DONT HAVE THE CODE OF FILTERING NOW //// BUT I CAN SEND IT LATER IF NECESSARY }
Quoted 3 lines
}
-------------- CODE FINISH HERE
Don't use sprintf(), its very expansive. Before the while loop put a pointer after path and / and then strcpy(ptr, pFile->d_name)
stat() is _very_ epansive! It means physical IO and fills up a structure with things you just don't need. If you really do need to filter out directories from your result do the stat after it has passed the filter.
Quoted 4 lines
Does have another mechanism to filter without using of
strcmp()/memcmp()? How if we dont know the size of extension (.c, .cpp, .teste, .longextension, and so on). ?
Compare them yourself with a pointer byte for byte. But the speed gain will not be so high as when you leave away the stat() call.
Holger
Quoted 5 lines
while ((pFile=readdir(pDir))!=NULL) {
sprintf(szBuf, "%s/%s", pPath, pFile->d_name);
stat(szBuf, &statFile);
if (S_ISDIR(statFile.st_mode))
continue;If the only reason you're statting the files is to check whether they're directories, then you can save yourself a system call per file:
while ((pFile=readdir(pDir))!=NULL) {
if (pFile->d_type == DT_DIR)
continue;This'll probably be quite a bit quicker, and may also save you needing to call sprintf(), which can be somewhat expensive.
Steven Smith.