1
\$\begingroup\$

I'm trying to learn C++ having come from a C/Assembly background. I thought one of the first tools I'd make as a project is a "directory tally" program:

  1. It will count all the files in the current directory
  2. If the user specifies, it will count all the folders in the current directory too
  3. If the user specifies, it will count all the files in subdirectories as well (and folders if the user specifies)

Given that most of my academic, professional, and personal programming projects have involved C instead of C++ I'm pretty sure that my code is not idiomatic C++ (yet), but I have used guidelines like use std::(w)string over (w)char(_t) * strings, use the STL as much as possible, etc.

I'm wondering if I can get feedback on:

  1. Is it obvious from looking at my code that I come from a C background?
  2. What can I do to make this program look more like idiomatic C++?
  3. The usual code review stuff, things I could write more clearly or bad things I'm doing that I should avoid.

#include <iostream>
#include <filesystem>

namespace fs = std::experimental::filesystem;

bool __cdecl recur_flag(std::wstring arg)
{
    if (arg.length() < 2) return false;
    return ((arg.at(0) == L'-' || arg.at(0) == L'/') && (arg.at(1) == L'r' || arg.at(1) == L'R'));
}

bool __cdecl dir_flag(std::wstring arg)
{
    if (arg.length() < 2) return false;
    return ((arg.at(0) == L'-' || arg.at(0) == L'/') && (arg.at(1) == L'd' || arg.at(1) == L'D'));
}

bool __cdecl help_flag(std::wstring arg)
{
    if (arg.length() < 2) return false;
    return ((arg.at(0) == L'-' || arg.at(0) == L'/') && (arg.at(1) == L'?'));
}



int __cdecl wmain(int argc, wchar_t *argv[])
{
    bool recur = false, dircount = false;
    unsigned long long count = 0;

    recur = ((argc >= 2) && (recur_flag(std::wstring(argv[1])))) || ((argc >= 3) && (recur_flag(std::wstring(argv[2]))));

    dircount = ((argc >= 2) && (dir_flag(std::wstring(argv[1])))) || ((argc >= 3) && (dir_flag(std::wstring(argv[2]))));

    if ((argc >= 2) && (help_flag(std::wstring(argv[1]))) || (argc >= 3) && (help_flag(std::wstring(argv[2]))))
    {
        std::wcout << L"Usage: dircount /R /D /?" << std::endl << L"\t/R Recursive - count files in current directory and subdirectories" << std::endl <<
            L"\t/D Include directories when counting files" << std::endl << L"\t/? Display this help message" << std::endl << std::endl;
        return 0;
    }

    if (recur)
    {
        for (auto &p : fs::recursive_directory_iterator(fs::current_path()))
        {
            if (fs::is_directory(p))
            {
                if (dircount) count++;
            }
            else count++;
        }
    }
    else
    {
            for (auto &p : fs::directory_iterator(fs::current_path()))
            {
                if (fs::is_directory(p))
                {
                    if (dircount) count++;
                }
                else count++;
            }
    }

    std::wcout << L"There are " << count << L" items in the current directory"; if (recur) std::wcout << L" tree"; std::wcout << std::endl;

    return 0;
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

I'm sure someone else can provide a more comprehensive answer than I could, Just a few things:

  • string::at throws an out-of-range exception, since you're not actually catching anywhere, I'd use [] since at() is slower and you're already checking the string length, not that it really matters that much for this simple code though.

  • I would factor out the std::wstring(argv[1]) and similar into local variables so there are fewer brackets.

  • if statements without braces are less maintainable.

  • wmain is windows specific, so this code won't compile elsewhere.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.