I need to perform a certain file operation in C++, like this:
- Find and delete all files in a given directory.
- Find if a given file exist, if yes, delete it, etc.
Please advise me a C++ solution/library which will work for both android and iOS platform.
C++ gives you provisions for file interaction.
I will show you my FileReader class which employs a bit of C style file handling using C++.
I will note that you probably want to avoid using C++ to list files in the directory if you can. Why can you not simply assume whether or not certain files will be there or not be there? I’ve done a bit of game programming and pretty much the only times you need to worry about the filesystem are for logging purposes or for loading assets. For both of these you can pretty much just assume what their paths are.
In addition, you may want to look at the remove function for deleting files. I can’t come up with any raw code in C++ for performing the task that
lsis meant for. I wouldn’t use C++ to do such a task (hint:lsis a pretty neat program).Also take a look at
statandopendir(thanks Ben) which should be available on your platforms. Another point to make is that a task such as listing files in a dir are generally things you’re gonna want to ask your OS kernel to do for you.A more high-level approach mentioned by another answerer is Boost Filesystem, which is a solid choice as Boost usually is: Take a look at this directory iteration example.
From a game programming perspective I’ve tended to lean on stuff like Lua’s
os(). For example if you have a Python program you could just do something likeos.system("ls")to get your dir contents assuming you have anlsprogram available.You could also
execthelsprogram from your C++ program.