How to read files in alphabetical in the local directory in Linux using C++

Hello,
I am new to Linux and is wondering how to read all files in the ./ directory (i.e. local) directory in alphabetical order in C++. I am aware of opendir() but how do you state that you want to read from the local directory. Any advice will be greatly appreciated!!!
readdir() gives you the directory entries in the order you get them. If you want to sort them you must do that yourself once you get the names.

opendir() takes as argument the path to the directory you want to open.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <algorithm>
#include <string>
#include <vector>

#include <dirent.h>
#include <sys/types.h>

// read_directory()
//   Return an ASCII-sorted vector of filename entries in a given directory.
//   If no path is specified, the current working directory is used.
//
//   Always check the value of the global 'errno' variable after using this
//   function to see if anything went wrong. (It will be zero if all is well.)
//
std::vector <std::string> read_directory( const std::string& path = std::string() )
  {
  std::vector <std::string> result;
  dirent* de;
  DIR* dp;
  errno = 0;
  dp = opendir( path.empty() ? "." : path.c_str() );
  if (dp)
    {
    while (true)
      {
      errno = 0;
      de = readdir( dp );
      if (de == NULL) break;
      result.push_back( std::string( de->d_name ) );
      }
    closedir( dp );
    std::sort( result.begin(), result.end() );
    }
  return result;
  }

Online documentation often gives good example code. For example:
http://www.opengroup.org/onlinepubs/007908775/xsh/opendir.html

Hope this helps.
Thanks!! This helped alot!! However, I have another question, if I go the above route will I be able to concatenate all files and parse the concatenated files and strip out all valid email addresses or do I need to go another way. Because I am thinking that the above way will just give me the filenames!! But while getting the filenames, is it posible to read the file and write it to one big temp file(which will be a file of all files). After reading and writng all files, the temp file can be read and all valid email addresses can be stipped out. Do you think this will work? If this will work, how do you read a file in linux. Once again thanks! Any advice will be greatly appreciated!!!
You can do whatever you want. Just remember that you can't do everything at once --that is, you can't skip steps.

1. Get the list of filenames.
2. Open the temp file for output.
3. Open each input file in turn and write it to the temp file.

You'll save yourself time and effort if you strip email addresses at the same time as you do step 3.

Open files using the normal C++ ifstream and ofstream classes.

You may also want to employ the stat() or lstat() function (#include <sys/stat.h>) to cull directory names, and links if you don't want them.
http://www.opengroup.org/onlinepubs/009695399/basedefs/sys/stat.h.html

Hope this helps.
Once again thanks!! After having time to look at your example(above) in detail, I have a couple of questions? First, if you have dp = opendir( ) instead of the above line, the search will be on the current/local directory,correct. Second, I don't understand the de->d_name in the following line of code: result.push_back( std::string( de->d_name ) ), exactly what is d_name? Your respone is greatly appreciated!!
First, no. It requires an argument.
Second, the filename.
The link to the opendir documentation will give you all the details you need.

Gets interesting fast, doesn't it?

:-)
Topic archived. No new replies allowed.