file handling

i want to pick up all the files from a directory one by one having a certain string in the name. thnx advnc...
Break it into steps.

1) Read all files in directory into suitable container. The best way to do this may depend on your operating system.
2) Examine each filename in the container; discard ones that do not match.

Start on that, and if you get stuck come back, show us how far you've got, and we'll point you forwards again.
thnx moschops...
actually i m new to c++ and i don't know much about operating system too. I m just studying them. i would really be obliged if you tell me which container you are telling me about and a little insight into it.
Well, you just need to make some kind of store of all the file names you read in, and do it in such a way that you can easily compare them with your search string.

One container is a vector. Containers can hold any kind of object you care to define. The obvious type to use here is a C++ standard string.

You could, for example, create a suitable vector like this:

vector<std::String> filesInDirectory;

Then, you could read each filename in turn and store each one into the vector. If you find that you are reading the filenames as C-style character arrays, you would convert them into std::string first before storing them.

Take a look at dirent.h for a means of gathering all the file names. There are other options. The Boost library for C++ comes with a very nice set of filesystem classes, for example.
Topic archived. No new replies allowed.