
谁能给个VS C++控制台程序下读取某个文件夹下的所有jpg文件的文件名?
3个回答
展开全部
记的加分!下面是代码,本人原创!
#include <string>
#include <list>
#include <iostream>
#include <Windows.h>
using namespace std;
int myDelete(string path)
{
WIN32_FIND_DATA findData;
HANDLE hHandle = NULL;
string filePathName;
string fullPathName;
filePathName = path;
filePathName += "\\*.*";
list<string> pathList;
hHandle = FindFirstFile(filePathName.data(), &findData);
if ( hHandle == INVALID_HANDLE_VALUE )
{
return 1;
}
do
{
if ( "." == (string)findData.cFileName || ".." == (string)findData.cFileName )
{
continue;
}
fullPathName = path;
fullPathName += "\\";
fullPathName += findData.cFileName;
if ( findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
myDelete(fullPathName.data());
}
else
{
string str = fullPathName.substr(fullPathName.length() - 3); //截取文件名后3位
if ( str == "jpg" )
{
pathList.push_back(fullPathName);
}
}
} while ( FindNextFile(hHandle, &findData) );
FindClose(hHandle);
list<string>::reverse_iterator rpos;
for ( rpos = pathList.rbegin(); rpos != pathList.rend(); rpos++)
{
cout << *rpos << endl;
}
return 0;
}
int main()
{
string path = "F:\\a";
myDelete(path);
return 0;
}
VS2008下编译 运行都没问题!
#include <string>
#include <list>
#include <iostream>
#include <Windows.h>
using namespace std;
int myDelete(string path)
{
WIN32_FIND_DATA findData;
HANDLE hHandle = NULL;
string filePathName;
string fullPathName;
filePathName = path;
filePathName += "\\*.*";
list<string> pathList;
hHandle = FindFirstFile(filePathName.data(), &findData);
if ( hHandle == INVALID_HANDLE_VALUE )
{
return 1;
}
do
{
if ( "." == (string)findData.cFileName || ".." == (string)findData.cFileName )
{
continue;
}
fullPathName = path;
fullPathName += "\\";
fullPathName += findData.cFileName;
if ( findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
myDelete(fullPathName.data());
}
else
{
string str = fullPathName.substr(fullPathName.length() - 3); //截取文件名后3位
if ( str == "jpg" )
{
pathList.push_back(fullPathName);
}
}
} while ( FindNextFile(hHandle, &findData) );
FindClose(hHandle);
list<string>::reverse_iterator rpos;
for ( rpos = pathList.rbegin(); rpos != pathList.rend(); rpos++)
{
cout << *rpos << endl;
}
return 0;
}
int main()
{
string path = "F:\\a";
myDelete(path);
return 0;
}
VS2008下编译 运行都没问题!
追问
谢谢了!但好还是有错误。在VS2008 下运行的。能否再修改一下?Error 2 error C2440: 'type cast' : cannot convert from 'WCHAR [260]' to 'std::string'
追答
问题的原因是字符串ANSI和Unicode编码的区别: VC6与VS2003等默认使用ANSI编码,而VS2008默认采用Unicode。简单的说,ANSI用1个字节表示字符,Unicode用2个字节表示1个字符。
在VS2008中,设置如下:
1、alt+F7,进入工程属性设置菜单。
2、在左边下拉菜单中,选择configuration properties --> General --> 在左边project default 中character set中设置 not set。
3、OK! 错误消失。

2023-06-12 广告
Android和ARM、Linux之间存在密切的联系。Android是一种基于Linux内核的嵌入式智能操作系统,它采用了ARM处理器作为其主要处理器架构。Android的内核和许多应用程序都是基于ARM架构编写的,包括处理器和内存管理器。...
点击进入详情页
本回答由浙江启扬智能科技有限公司提供
展开全部
这是读取某个文件夹下所有文件名的程序,linux下C语言的,VS C++里应该也能用,读取后自己判断是否以.jpg结尾,OK。
#include<stdio.h>
#include<stdlib.h>
#include <sys/types.h>
#include <dirent.h>
int main(int argc,char **argv)
{
DIR * dir;
dir=opendir(".");
struct dirent *drt;
while((drt=readdir(dir))!=NULL)
{
if(drt->d_name[0]!='.')
printf("%s%s",drt->d_name," \n");
}
closedir(dir);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include <sys/types.h>
#include <dirent.h>
int main(int argc,char **argv)
{
DIR * dir;
dir=opendir(".");
struct dirent *drt;
while((drt=readdir(dir))!=NULL)
{
if(drt->d_name[0]!='.')
printf("%s%s",drt->d_name," \n");
}
closedir(dir);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询