统计当前目录中给出的扩展名的文件个数。 #include <stdio.h> #include <string.h> #include <io.h> #include <time.h> #include <conio.h> // extra name FILE *fpFile = NULL; void RemoveExtName(char *pFileExtName) { int nNameLen = strlen(pFileExtName); for (; nNameLen >= 0; nNameLen--) { if (pFileExtName[nNameLen] != '.') { pFileExtName[nNameLen] = 0; } else { pFileExtName[nNameLen] = 0; break; } } } int main(void) { struct _finddata_t tTmpFile; long hFile; char chFileType[10] = {0}; int nCount = 0; printf("Input your fild file type: "); scanf("%s", chFileType); /* Find first *.x file in current directory */ hFile = _findfirst(chFileType, &tTmpFile); if (-1L == hFile) { printf( "No %s files in current directory!/n", chFileType); } else { printf( "/nListing of all files/n/n" ); fpFile = fopen("temp_file.txt", "w"); if (NULL == fpFile) { printf("can not create temp_file.txt"); } nCount++; /* Find the rest of the *.x files */ while(0 == _findnext(hFile, &tTmpFile)) { printf(" %-12s/n", tTmpFile.name); RemoveExtName(tTmpFile.name); fprintf(fpFile, tTmpFile.name); fprintf(fpFile, "/n"); nCount++; } _findclose(hFile); fclose(fpFile); } printf("/ntotal %s number is: %d/n", chFileType, nCount); getch(); return 0; }