写一个Win32控制台程序 要求读取一个文件夹里的所有文件 在网上查了一种方法如下:////////////////////////////////////////////////////////////////////////
bool readFiles(char * dirPath)
{
CFileFind ff;
CString filePath;
CString szDir = CString(dirPath);
if(szDir.Right(1) != "\\")
szDir += "\\"; szDir += "*.*";
BOOL res = ff.FindFile(szDir);
while( res )
{
res = ff.FindNextFile();
if(ff.IsDirectory() && !ff.IsDots())
{
continue;
}
else if(!ff.IsDirectory() && !ff.IsDots())
{
filePath = ff.GetFilePath();
if(filePath.Right(3).CompareNoCase("cpp")) //要找的文件后缀
{
cout<<filePath<<endl;
continue;
}
} }
ff.Close(); return true;
} ///////////////////////////////////////////////////////////////////////////////////////////////
可是编译在链接时老是报错,已经#include<afx.h>,因为CFileFind 在<afx.h>中定义,报错情况如下:Compiling...
read.cpp
Linking...
nafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in libcpd.lib(delop.obj)
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex
Debug/readfile.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.甚是不解,是否由于控制台不能#include<afx.h>的原因,望高手帮忙解决
谢谢.....
bool readFiles(char * dirPath)
{
CFileFind ff;
CString filePath;
CString szDir = CString(dirPath);
if(szDir.Right(1) != "\\")
szDir += "\\"; szDir += "*.*";
BOOL res = ff.FindFile(szDir);
while( res )
{
res = ff.FindNextFile();
if(ff.IsDirectory() && !ff.IsDots())
{
continue;
}
else if(!ff.IsDirectory() && !ff.IsDots())
{
filePath = ff.GetFilePath();
if(filePath.Right(3).CompareNoCase("cpp")) //要找的文件后缀
{
cout<<filePath<<endl;
continue;
}
} }
ff.Close(); return true;
} ///////////////////////////////////////////////////////////////////////////////////////////////
可是编译在链接时老是报错,已经#include<afx.h>,因为CFileFind 在<afx.h>中定义,报错情况如下:Compiling...
read.cpp
Linking...
nafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in libcpd.lib(delop.obj)
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex
Debug/readfile.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.甚是不解,是否由于控制台不能#include<afx.h>的原因,望高手帮忙解决
谢谢.....
#include <windows.h>
#include <stdio.h>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))
{
printf("Couldn't create new directory.");
return;
}
// Start searching for .TXT files in the current directory.
hSearch = FindFirstFile("*.txt", &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
{
printf("No .TXT files found.");
return;
}
// 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
{
printf("Couldn't copy file.");
return;
}
if (!FindNextFile(hSearch, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
MessageBox(hwnd, "No more .TXT files.",
"Search completed.", MB_OK);
fFinished = TRUE;
}
else
{
printf("Couldn't find next file.");
return;
}
}
}
// Close the search handle.
FindClose(hSearch);
选择带MFC支持的
在 stdafx.h 中包含
#include <iostream>
using namespace std;