12 个解决方案
#1
#2
findfirst, findnext
遍历目录和文件的函数,判断如果是目录继续遍历。
遍历目录和文件的函数,判断如果是目录继续遍历。
#3
#include <windows.h>
#include <iostream>
#define FILEILTER "*.*"
BOOL IsRoot(LPCTSTR lpszPath)
{
TCHAR szRoot[4];
wsprintf(szRoot, "%c:\\", lpszPath[0]);
return (lstrcmp(szRoot, lpszPath) == 0);
}
void FindInAll(LPCTSTR lpszPath)
{
TCHAR szFind[MAX_PATH];
lstrcpy(szFind, lpszPath);
if (!IsRoot(szFind))
lstrcat(szFind, "\\");
lstrcat(szFind, FILEILTER); // 找所有文件
WIN32_FIND_DATA wfd;
HANDLE hFind = FindFirstFile(szFind, &wfd);
if (hFind == INVALID_HANDLE_VALUE) // 如果没有找到或查找失败
return;
do
{
if (wfd.cFileName[0] == '.')
continue; // 过滤这两个目录
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
TCHAR szFile[MAX_PATH];
if (IsRoot(lpszPath))
wsprintf(szFile, "%s%s", lpszPath, wfd.cFileName);
else
{
wsprintf(szFile, "%s\\%s", lpszPath, wfd.cFileName);
FindInAll(szFile); // 如果找到的是目录,则进入此目录进行递归
}
}
else
{
TCHAR szFile[MAX_PATH];
if (IsRoot(lpszPath))
{
wsprintf(szFile, "%s%s", lpszPath, wfd.cFileName);
}
else
{
wsprintf(szFile, "%s\\%s", lpszPath, wfd.cFileName);
printf("%s\n",szFile);
}
// 对文件进行操作
}
} while (FindNextFile(hFind, &wfd));
FindClose(hFind); // 关闭查找句柄
}
int main(int argc, char* argv[])
{
FindInAll("E:\\Book\\c++ 书籍");
return 0;
}
#include <iostream>
#define FILEILTER "*.*"
BOOL IsRoot(LPCTSTR lpszPath)
{
TCHAR szRoot[4];
wsprintf(szRoot, "%c:\\", lpszPath[0]);
return (lstrcmp(szRoot, lpszPath) == 0);
}
void FindInAll(LPCTSTR lpszPath)
{
TCHAR szFind[MAX_PATH];
lstrcpy(szFind, lpszPath);
if (!IsRoot(szFind))
lstrcat(szFind, "\\");
lstrcat(szFind, FILEILTER); // 找所有文件
WIN32_FIND_DATA wfd;
HANDLE hFind = FindFirstFile(szFind, &wfd);
if (hFind == INVALID_HANDLE_VALUE) // 如果没有找到或查找失败
return;
do
{
if (wfd.cFileName[0] == '.')
continue; // 过滤这两个目录
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
TCHAR szFile[MAX_PATH];
if (IsRoot(lpszPath))
wsprintf(szFile, "%s%s", lpszPath, wfd.cFileName);
else
{
wsprintf(szFile, "%s\\%s", lpszPath, wfd.cFileName);
FindInAll(szFile); // 如果找到的是目录,则进入此目录进行递归
}
}
else
{
TCHAR szFile[MAX_PATH];
if (IsRoot(lpszPath))
{
wsprintf(szFile, "%s%s", lpszPath, wfd.cFileName);
}
else
{
wsprintf(szFile, "%s\\%s", lpszPath, wfd.cFileName);
printf("%s\n",szFile);
}
// 对文件进行操作
}
} while (FindNextFile(hFind, &wfd));
FindClose(hFind); // 关闭查找句柄
}
int main(int argc, char* argv[])
{
FindInAll("E:\\Book\\c++ 书籍");
return 0;
}
#4
参考:
http://topic.csdn.net/t/20020620/00/816941.html
http://topic.csdn.net/t/20020620/00/816941.html
#5
#6
3楼正解............
#7
楼主真学的C++?没学到stl这个强力工具?
#8
路径由命令行参数输入
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<dirent.h>
void SearchDir(char *szDir){
DIR *dp;
struct dirent *pDirent;
struct stat fbuf;
char szOut[256];
FILE* fp;
dp=opendir(szDir);
if(!dp)return;
chdir(szDir);
if((fp=fopen("./file.txt","w"))==NULL){
perror("fopen");
return;
}
close(1);
dup2(fileno(fp),fileno(stdout)); /*输出重定向到file.txt*/
while((pDirent=readdir(dp))){
memset(&fbuf,0x00,sizeof(fbuf));
stat(pDirent->d_name,&fbuf); /*获取目录项信息*/
/*判断是否是目录并且是"."和".."*/
if(S_ISDIR(fbuf.st_mode) && memcmp(pDirent->d_name,".",1)!=0
&& memcmp(pDirent->d_name,"..",2)!=0){
memset(szOut,0x00,sizeof(szOut));
sprintf(szOut,"%s/%-s",szDir,pDirent->d_name);/*输出目录名到szOut,以便遍历其子目录*/
sprintf("%s/%-s\n",szDir,pDirent->d_name);/*输出目录名到file.txt*/
SearchDir(szOut); /*递归遍历其子目录*/
}
}
chdir("..");
closedir(dp);
return;
}
int main(int argc,char **argv){
if(argc<2)
exit(1);
SearchDir(argv[1]);/*遍历命令行参数1指定目录*/
return 0;
}
#9
CFileFind
#10
//来自msdn例子:
WIN32_FIND_DATA FileData;
HANDLE hSearch;
DWORD dwAttrs;
char szDirPath[] = "c:\\TEXTRO\\";
char szNewPath[MAX_PATH];
char szHome[MAX_PATH];
BOOL fFinished = FALSE;
// Create a new directory.
if (!CreateDirectory(szDirPath, NULL))
{
ErrorHandler("Couldn't create new directory.");
}
// Start searching for .TXT files in the current directory.
hSearch = FindFirstFile("*.txt", &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
{
ErrorHandler("No .TXT files found.");
}
// Copy each .TXT file to the new directory
// and change it to read only, if not already.
while (!fFinished)
{
lstrcpy(szNewPath, szDirPath);
lstrcat(szNewPath, FileData.cFileName);
if (CopyFile(FileData.cFileName, szNewPath, FALSE))
{
dwAttrs = GetFileAttributes(FileData.cFileName);
if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))
{
SetFileAttributes(szNewPath,
dwAttrs | FILE_ATTRIBUTE_READONLY);
}
}
else
{
ErrorHandler("Couldn't copy file.");
}
if (!FindNextFile(hSearch, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
MessageBox(hwnd, "No more .TXT files.",
"Search completed.", MB_OK);
fFinished = TRUE;
}
else
{
ErrorHandler("Couldn't find next file.");
}
}
}
// Close the search handle.
if (!FindClose(hSearch))
{
ErrorHandler("Couldn't close search handle.");
}
WIN32_FIND_DATA FileData;
HANDLE hSearch;
DWORD dwAttrs;
char szDirPath[] = "c:\\TEXTRO\\";
char szNewPath[MAX_PATH];
char szHome[MAX_PATH];
BOOL fFinished = FALSE;
// Create a new directory.
if (!CreateDirectory(szDirPath, NULL))
{
ErrorHandler("Couldn't create new directory.");
}
// Start searching for .TXT files in the current directory.
hSearch = FindFirstFile("*.txt", &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
{
ErrorHandler("No .TXT files found.");
}
// Copy each .TXT file to the new directory
// and change it to read only, if not already.
while (!fFinished)
{
lstrcpy(szNewPath, szDirPath);
lstrcat(szNewPath, FileData.cFileName);
if (CopyFile(FileData.cFileName, szNewPath, FALSE))
{
dwAttrs = GetFileAttributes(FileData.cFileName);
if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))
{
SetFileAttributes(szNewPath,
dwAttrs | FILE_ATTRIBUTE_READONLY);
}
}
else
{
ErrorHandler("Couldn't copy file.");
}
if (!FindNextFile(hSearch, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
MessageBox(hwnd, "No more .TXT files.",
"Search completed.", MB_OK);
fFinished = TRUE;
}
else
{
ErrorHandler("Couldn't find next file.");
}
}
}
// Close the search handle.
if (!FindClose(hSearch))
{
ErrorHandler("Couldn't close search handle.");
}
#11
system("cmd /c dir /a-d /b /s C:\\data\\*.dat >c:\\alldatfiles.txt");
//然后读文件c:\\alldatfiles.txt的内容
//然后读文件c:\\alldatfiles.txt的内容
#12
google一下有
#1
#2
findfirst, findnext
遍历目录和文件的函数,判断如果是目录继续遍历。
遍历目录和文件的函数,判断如果是目录继续遍历。
#3
#include <windows.h>
#include <iostream>
#define FILEILTER "*.*"
BOOL IsRoot(LPCTSTR lpszPath)
{
TCHAR szRoot[4];
wsprintf(szRoot, "%c:\\", lpszPath[0]);
return (lstrcmp(szRoot, lpszPath) == 0);
}
void FindInAll(LPCTSTR lpszPath)
{
TCHAR szFind[MAX_PATH];
lstrcpy(szFind, lpszPath);
if (!IsRoot(szFind))
lstrcat(szFind, "\\");
lstrcat(szFind, FILEILTER); // 找所有文件
WIN32_FIND_DATA wfd;
HANDLE hFind = FindFirstFile(szFind, &wfd);
if (hFind == INVALID_HANDLE_VALUE) // 如果没有找到或查找失败
return;
do
{
if (wfd.cFileName[0] == '.')
continue; // 过滤这两个目录
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
TCHAR szFile[MAX_PATH];
if (IsRoot(lpszPath))
wsprintf(szFile, "%s%s", lpszPath, wfd.cFileName);
else
{
wsprintf(szFile, "%s\\%s", lpszPath, wfd.cFileName);
FindInAll(szFile); // 如果找到的是目录,则进入此目录进行递归
}
}
else
{
TCHAR szFile[MAX_PATH];
if (IsRoot(lpszPath))
{
wsprintf(szFile, "%s%s", lpszPath, wfd.cFileName);
}
else
{
wsprintf(szFile, "%s\\%s", lpszPath, wfd.cFileName);
printf("%s\n",szFile);
}
// 对文件进行操作
}
} while (FindNextFile(hFind, &wfd));
FindClose(hFind); // 关闭查找句柄
}
int main(int argc, char* argv[])
{
FindInAll("E:\\Book\\c++ 书籍");
return 0;
}
#include <iostream>
#define FILEILTER "*.*"
BOOL IsRoot(LPCTSTR lpszPath)
{
TCHAR szRoot[4];
wsprintf(szRoot, "%c:\\", lpszPath[0]);
return (lstrcmp(szRoot, lpszPath) == 0);
}
void FindInAll(LPCTSTR lpszPath)
{
TCHAR szFind[MAX_PATH];
lstrcpy(szFind, lpszPath);
if (!IsRoot(szFind))
lstrcat(szFind, "\\");
lstrcat(szFind, FILEILTER); // 找所有文件
WIN32_FIND_DATA wfd;
HANDLE hFind = FindFirstFile(szFind, &wfd);
if (hFind == INVALID_HANDLE_VALUE) // 如果没有找到或查找失败
return;
do
{
if (wfd.cFileName[0] == '.')
continue; // 过滤这两个目录
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
TCHAR szFile[MAX_PATH];
if (IsRoot(lpszPath))
wsprintf(szFile, "%s%s", lpszPath, wfd.cFileName);
else
{
wsprintf(szFile, "%s\\%s", lpszPath, wfd.cFileName);
FindInAll(szFile); // 如果找到的是目录,则进入此目录进行递归
}
}
else
{
TCHAR szFile[MAX_PATH];
if (IsRoot(lpszPath))
{
wsprintf(szFile, "%s%s", lpszPath, wfd.cFileName);
}
else
{
wsprintf(szFile, "%s\\%s", lpszPath, wfd.cFileName);
printf("%s\n",szFile);
}
// 对文件进行操作
}
} while (FindNextFile(hFind, &wfd));
FindClose(hFind); // 关闭查找句柄
}
int main(int argc, char* argv[])
{
FindInAll("E:\\Book\\c++ 书籍");
return 0;
}
#4
参考:
http://topic.csdn.net/t/20020620/00/816941.html
http://topic.csdn.net/t/20020620/00/816941.html
#5
#6
3楼正解............
#7
楼主真学的C++?没学到stl这个强力工具?
#8
路径由命令行参数输入
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<dirent.h>
void SearchDir(char *szDir){
DIR *dp;
struct dirent *pDirent;
struct stat fbuf;
char szOut[256];
FILE* fp;
dp=opendir(szDir);
if(!dp)return;
chdir(szDir);
if((fp=fopen("./file.txt","w"))==NULL){
perror("fopen");
return;
}
close(1);
dup2(fileno(fp),fileno(stdout)); /*输出重定向到file.txt*/
while((pDirent=readdir(dp))){
memset(&fbuf,0x00,sizeof(fbuf));
stat(pDirent->d_name,&fbuf); /*获取目录项信息*/
/*判断是否是目录并且是"."和".."*/
if(S_ISDIR(fbuf.st_mode) && memcmp(pDirent->d_name,".",1)!=0
&& memcmp(pDirent->d_name,"..",2)!=0){
memset(szOut,0x00,sizeof(szOut));
sprintf(szOut,"%s/%-s",szDir,pDirent->d_name);/*输出目录名到szOut,以便遍历其子目录*/
sprintf("%s/%-s\n",szDir,pDirent->d_name);/*输出目录名到file.txt*/
SearchDir(szOut); /*递归遍历其子目录*/
}
}
chdir("..");
closedir(dp);
return;
}
int main(int argc,char **argv){
if(argc<2)
exit(1);
SearchDir(argv[1]);/*遍历命令行参数1指定目录*/
return 0;
}
#9
CFileFind
#10
//来自msdn例子:
WIN32_FIND_DATA FileData;
HANDLE hSearch;
DWORD dwAttrs;
char szDirPath[] = "c:\\TEXTRO\\";
char szNewPath[MAX_PATH];
char szHome[MAX_PATH];
BOOL fFinished = FALSE;
// Create a new directory.
if (!CreateDirectory(szDirPath, NULL))
{
ErrorHandler("Couldn't create new directory.");
}
// Start searching for .TXT files in the current directory.
hSearch = FindFirstFile("*.txt", &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
{
ErrorHandler("No .TXT files found.");
}
// Copy each .TXT file to the new directory
// and change it to read only, if not already.
while (!fFinished)
{
lstrcpy(szNewPath, szDirPath);
lstrcat(szNewPath, FileData.cFileName);
if (CopyFile(FileData.cFileName, szNewPath, FALSE))
{
dwAttrs = GetFileAttributes(FileData.cFileName);
if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))
{
SetFileAttributes(szNewPath,
dwAttrs | FILE_ATTRIBUTE_READONLY);
}
}
else
{
ErrorHandler("Couldn't copy file.");
}
if (!FindNextFile(hSearch, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
MessageBox(hwnd, "No more .TXT files.",
"Search completed.", MB_OK);
fFinished = TRUE;
}
else
{
ErrorHandler("Couldn't find next file.");
}
}
}
// Close the search handle.
if (!FindClose(hSearch))
{
ErrorHandler("Couldn't close search handle.");
}
WIN32_FIND_DATA FileData;
HANDLE hSearch;
DWORD dwAttrs;
char szDirPath[] = "c:\\TEXTRO\\";
char szNewPath[MAX_PATH];
char szHome[MAX_PATH];
BOOL fFinished = FALSE;
// Create a new directory.
if (!CreateDirectory(szDirPath, NULL))
{
ErrorHandler("Couldn't create new directory.");
}
// Start searching for .TXT files in the current directory.
hSearch = FindFirstFile("*.txt", &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
{
ErrorHandler("No .TXT files found.");
}
// Copy each .TXT file to the new directory
// and change it to read only, if not already.
while (!fFinished)
{
lstrcpy(szNewPath, szDirPath);
lstrcat(szNewPath, FileData.cFileName);
if (CopyFile(FileData.cFileName, szNewPath, FALSE))
{
dwAttrs = GetFileAttributes(FileData.cFileName);
if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))
{
SetFileAttributes(szNewPath,
dwAttrs | FILE_ATTRIBUTE_READONLY);
}
}
else
{
ErrorHandler("Couldn't copy file.");
}
if (!FindNextFile(hSearch, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
MessageBox(hwnd, "No more .TXT files.",
"Search completed.", MB_OK);
fFinished = TRUE;
}
else
{
ErrorHandler("Couldn't find next file.");
}
}
}
// Close the search handle.
if (!FindClose(hSearch))
{
ErrorHandler("Couldn't close search handle.");
}
#11
system("cmd /c dir /a-d /b /s C:\\data\\*.dat >c:\\alldatfiles.txt");
//然后读文件c:\\alldatfiles.txt的内容
//然后读文件c:\\alldatfiles.txt的内容
#12
google一下有