Why is C++ on linux basically C? I've tried everything to avoid C on linux but it's simply near impossible. I've tried to stick directly to the standard library but I somehow find myself using dirent.h, types.h, etc..

Is there any other ways to do these other than using C or WinAPI? Or am I just going to have to live with it and merge both of them using #ifdef _WIN32? I'm using Ubuntu 12.04 with g++ and Codeblocks. There's barely any documentation of programming c++ on linux :S I've searched and every time I end up at stackoverflow or "Linux equivalent of..". For bitmaps, I had to create all the structures, types, etc.. There has to be an easier way than this?

Anyway is there any other way to do this with just c++?

#include <stdio.h>
#include <dirent.h>

int listdir(const char *path) {
  struct dirent *entry;
  DIR *dp;

  dp = opendir(path);
  if (dp == NULL) {
    perror("opendir: Path does not exist or could not be read.");
    return -1;
  }

  while ((entry = readdir(dp)))
    Result.push_back(entry->d_name);

  closedir(dp);
  return 0;
}

Versus:

std::vector<std::string> SearchDirectory(std::string RootDirectory, std::string FileExtension, bool SearchSubdirectories, bool FullPath, bool IncludeFolders)
{
    std::vector<std::string> FilesFound;             // Result
    std::string  FilePath;              // Filepath
    std::string  Pattern;               // Pattern
    std::string  Extension;             // Extension
    HANDLE  hFile;                      // Handle to file
    WIN32_FIND_DATA FileInformation;    // File information

    Pattern = RootDirectory + "/*.*";

    hFile = FindFirstFile(Pattern.c_str(), &FileInformation);
    if(hFile != INVALID_HANDLE_VALUE)
    {
        Repeat
        if(FileInformation.cFileName[0] != '.')
        {
            FilePath.erase();
            FilePath = (FullPath ? RootDirectory + "/" + FileInformation.cFileName : FileInformation.cFileName);

            if(FileInformation.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY)
            {
                if (IncludeFolders)
                    FilesFound.push_back(FilePath);

                if(SearchSubdirectories)
                {
                    // Search subdirectory
                    std::vector<std::string> Temp = SearchDirectory(FilePath, FileExtension, FullPath, SearchSubdirectories);
                    for (size_t I = 0; I < Temp.size(); I++)
                    {
                        FilesFound.push_back(Temp[I]);
                    }
                    Temp.clear();
                }
            }
            else
            {
                //Check extension
                Extension = FileInformation.cFileName;
                Extension = Extension.substr(Extension.rfind(".") + 1);

                if (!FileExtension.empty() && (Extension == FileExtension))
                    FilesFound.push_back(FilePath);   //Save filename
                else if (FileExtension.empty())
                    FilesFound.push_back(FilePath);
            }
        }
        Until(FindNextFile(hFile, &FileInformation) != TRUE);

        FindClose(hFile);   //CloseFile Handle
    }
    return FilesFound;
}

Hmm I was really hoping to not have to add anything extra but if that's the only way then I guess I'll have to do that.

Why is there so little support for plain C++ on linux though? :S

Why is there so little support for plain C++ on linux though? :S

I find it funny that you complain about lack of C++ "support" by the POSIX libraries, then turn around and compare it to the Win32 API (itself a C interface). What makes you think that FindFirstFile()/FindNextFile() and the WIN32_FIND_DATA structure are any less "C" than opendir()/readdir() and the DIR structure?

Welcome to multi-platform. Things that are not specified by the standard (such as directory traversal) are left to system implementors.
You may get some support from standards like POSIX but even then, you are back to requiring POSIX.

I find it funny that you complain about lack of C++ "support" by the POSIX libraries, then turn around and compare it to the Win32 API (itself a C interface).

But at least it's widely documented and doesn't have a horrible naming convention. I've never seen Dirent.h or any of those includes before.

Welcome to multi-platform

Things that are not specified by the standard (such as directory traversal) are left to system implementors.

That explains it! I'll come up with my own implementation or just use boost like ancient dragon suggested.

I've never seen Dirent.h or any of those includes before.

You will see them a lot when you do *nix programming. Just like you see windows.h when doing Ms-Windows programming. C is the standard implementation of os interface functions because almost all other languages can call them. c++ functions can only be called by c++.

But at least it's widely documented and doesn't have a horrible naming convention.

We may disagree a bit on what constitutes a horrible naming convention. ;) I won't try to defend the conventions from POSIX, but Win32 names are either excessively verbose or annoyingly hungarian.

I've never seen Dirent.h or any of those includes before.

They've been around for decades. I take it you've worked primarily with Windows systems and only recently moved to Linux? There's going to be a learning curve because even though the basic concepts are similar, the concrete details are different between operating systems.

commented: +1 I just moved to linux yes. I <3 Hungarian & CamelCase conventions lol. +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.