Hi!
So, I need to list all the files in the current directory, i have tried several methods but i keep getting this error: cannot convert `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to `const char*' for argument `1' to `int system(const char*)'
What is the best method to do this?
In any case, heres what i have so far...
#include <stdlib.h>
#include <string>
using namespace std;
What do you mean by "best"? The code you have posted will work on any system that recognises the DIR command in the shell (once you solve that string to char* problem). Which, I think, is windows.
The last time I had to do this, I used the dirent.h header file and the readdir function, but dirent.h is POSIX and isn't part of windows.
I think windows makes use of FindFirstFile and FindNextFile to do something similar.
What happens if you try to build that system command in pieces?
The error you listed above indicates that you have a std::String, and you're trying to feed it to something that expects a const char*. This makes sense, as you are using the operator '+' on a char* and a std::string, and the return from that is a std::string, which you are then trying to feed to the system command, which expects a const char*.
Look at the std::string member function c_str()
Last edited by Moschops; January 19th, 2011 at 12:19 PM.
Heres an exe of the above code you gave me, it works, but it says Cannot Find Specified Path?
Even though the path is correct... I'm about to try dirent.h, ill reply whether or not i have any success
Heres an exe of the above code you gave me, it works, but it says Cannot Find Specified Path?
Even though the path is correct... I'm about to try dirent.h, ill reply whether or not i have any success
How do you know the path is correct? If it cannot find the specified path, that would imply that the path must be incorrect.
How do you know the path is correct? If it cannot find the specified path, that would imply that the path must be incorrect.
I think its getting stuck on the spaces because if i try doing a dir of for exampleC:\Documents and Settings\Guest\Application Data\Mozilla\Firefox it'll report cannot find path specified but C:\unervx\docs will work perfectly :P
I dont know, going to try dirent.h
For an easy-to-use and cross-platform library for doing such things, I recommend Boost.Filesystem. I'm not sure whether it's going to be included in C++0x or not.
You could make a 'system("dir .");' to list the current directory. As long as your program wouldn't do a change of the working directory programmatically, the folder listed is the one where the executable resides.
You also could do 'system("dir . >> dir.txt");' what redirects the output to file dir.txt (which also is in the current folder). After that you could open the dir.txt and make a customized output or apply a filtering of the files listed or do whatever you want. I sometimes write batch files this way by using the files listed as input for further commands.
An alternative to the system command is to using FindFirstFile and FindNextFile of WINAPI.
You would find a lot of code samples of that but for your case it is as simple as
Code:
WIN32_FIND_DATA fd = { 0 };
HANDLE hf = FindFirstFile("*.*", &fd);
if (hf == INVALID_HANDLE_VALUE) return GetLastError();
do
{
std::cout << fd.cFileName << std::endl;
} while (FindNextFile(hf, &fd));
#include <dirent.h>
static bool Exists(const string & name){
if (DIR * dir = opendir(name.c_str())){
closedir(dir);
return true;
}
return false;
}
//name should include the final slash
static bool GetAllFiles(const string & name, vector<string> & res, bool recurse){
if (DIR * dir = opendir(name.c_str())){
struct dirent * dp;
if (!(dp = readdir(dir))) return false; //.
if (!(dp = readdir(dir))) return false; //..
while(dp = readdir(dir)){
const string str = name + dp -> d_name;
if (Exists(str + "/")){
if (recurse) GetAllFiles(str + "/", res, true);
} else {
res.push_back(str);
}
}
closedir(dir);
return true;
}
return false;
}
Works really well, you should only have problems if dirent.h doesn't exist, but it should be there on all *nix systems and MinGW. I'm not entirely sure if you have to free readdir or not :/ Can someone confirm that you don't have to? I think it's one of those things like ctime where you don't have to.
Last edited by ninja9578; January 19th, 2011 at 06:35 PM.
For an easy-to-use and cross-platform library for doing such things, I recommend Boost.Filesystem. I'm not sure whether it's going to be included in C++0x or not.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.