由于及调用里面的函数在linux下会报错,所以linux下用另外的方法来实现。
#include #include #include #include #include #include
using namespace std;
int main()
{
vectorfile_name;
string path = "D:\\test\\";
GetFileNames(path, file_name);
return 0;
}
void GetFileNames(string path,vector& filenames)
{
DIR *pDir;
struct dirent* ptr;
if(!(pDir = opendir(path.c_str()))){
cout< return;
}
while((ptr = readdir(pDir))!=0) {
if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0){
filenames.push_back(path + "/" + ptr->d_name);
}
}
closedir(pDir);
GetLineAndPrint(filenames);
}
void GetLineAndPrint(vector& filenames)
{
for (vector::iterator iter = filenames.begin(); iter != filenames.end(); iter++){
ifstream infile_name(*iter);
if (infile_name){
string str;
while (getline(infile_name, str)){
cout << str << endl;
}
}
}
}