Skip to main content
added 247 characters in body
Source Link
Chris
  • 10.3k
  • 1
  • 9
  • 66

Error reporting

To properly replicate the functionality of wc the error in this loop should print to std::cerr rather than std::cout.

    /* Loop over all the specified files */
    for (auto fileName: files) {
        std::ifstream   file(fileName);
        if (!file) {
            std::cout << "Unknown file: " << fileName << "\n";
        }
        else {
            display(file, fileName, options);
        }
    }

As I can see on my test machine:

% wc foo
wc: foo: open: No such file or directory
% wc foo 2> /dev/null 
% 

Fortunately, it's about the simples fix imaginable. All of three characters.

Since there are multiple reasons your program might fail to open a file, you should either:

  • Investigate further and identify the actual source of the error, or...
  • Use a more generic error message like "failure to open file {filename}".

Strings

Consider std::string_view vs. const std::string&.

From Stack Overflow: How exactly is std::string_view faster than const std::string&?

Error reporting

To properly replicate the functionality of wc the error in this loop should print to std::cerr rather than std::cout.

    /* Loop over all the specified files */
    for (auto fileName: files) {
        std::ifstream   file(fileName);
        if (!file) {
            std::cout << "Unknown file: " << fileName << "\n";
        }
        else {
            display(file, fileName, options);
        }
    }

As I can see on my test machine:

% wc foo
wc: foo: open: No such file or directory
% wc foo 2> /dev/null 
% 

Fortunately, it's about the simples fix imaginable. All of three characters.

Strings

Consider std::string_view vs. const std::string&.

From Stack Overflow: How exactly is std::string_view faster than const std::string&?

Error reporting

To properly replicate the functionality of wc the error in this loop should print to std::cerr rather than std::cout.

    /* Loop over all the specified files */
    for (auto fileName: files) {
        std::ifstream   file(fileName);
        if (!file) {
            std::cout << "Unknown file: " << fileName << "\n";
        }
        else {
            display(file, fileName, options);
        }
    }

As I can see on my test machine:

% wc foo
wc: foo: open: No such file or directory
% wc foo 2> /dev/null 
% 

Fortunately, it's about the simples fix imaginable. All of three characters.

Since there are multiple reasons your program might fail to open a file, you should either:

  • Investigate further and identify the actual source of the error, or...
  • Use a more generic error message like "failure to open file {filename}".

Strings

Consider std::string_view vs. const std::string&.

From Stack Overflow: How exactly is std::string_view faster than const std::string&?

added 264 characters in body
Source Link
Chris
  • 10.3k
  • 1
  • 9
  • 66

Error reporting

To properly replicate the functionality of wc the error in this loop should print to std::cerr rather than std::cout.

    /* Loop over all the specified files */
    for (auto fileName: files) {
        std::ifstream   file(fileName);
        if (!file) {
            std::cout << "Unknown file: " << fileName << "\n";
        }
        else {
            display(file, fileName, options);
        }
    }

As I can see on my test machine:

% wc foo
wc: foo: open: No such file or directory
% wc foo 2> /dev/null 
% 

Fortunately, it's about the simples fix imaginable. All of three characters.

Strings

Consider std::string_view vs. const std::string&.

From Stack Overflow: How exactly is std::string_view faster than const std::string&?

Error reporting

To properly replicate the functionality of wc the error in this loop should print to std::cerr rather than std::cout.

    /* Loop over all the specified files */
    for (auto fileName: files) {
        std::ifstream   file(fileName);
        if (!file) {
            std::cout << "Unknown file: " << fileName << "\n";
        }
        else {
            display(file, fileName, options);
        }
    }

As I can see on my test machine:

% wc foo
wc: foo: open: No such file or directory
% wc foo 2> /dev/null 
% 

Fortunately, it's about the simples fix imaginable. All of three characters.

Error reporting

To properly replicate the functionality of wc the error in this loop should print to std::cerr rather than std::cout.

    /* Loop over all the specified files */
    for (auto fileName: files) {
        std::ifstream   file(fileName);
        if (!file) {
            std::cout << "Unknown file: " << fileName << "\n";
        }
        else {
            display(file, fileName, options);
        }
    }

As I can see on my test machine:

% wc foo
wc: foo: open: No such file or directory
% wc foo 2> /dev/null 
% 

Fortunately, it's about the simples fix imaginable. All of three characters.

Strings

Consider std::string_view vs. const std::string&.

From Stack Overflow: How exactly is std::string_view faster than const std::string&?

Source Link
Chris
  • 10.3k
  • 1
  • 9
  • 66

Error reporting

To properly replicate the functionality of wc the error in this loop should print to std::cerr rather than std::cout.

    /* Loop over all the specified files */
    for (auto fileName: files) {
        std::ifstream   file(fileName);
        if (!file) {
            std::cout << "Unknown file: " << fileName << "\n";
        }
        else {
            display(file, fileName, options);
        }
    }

As I can see on my test machine:

% wc foo
wc: foo: open: No such file or directory
% wc foo 2> /dev/null 
% 

Fortunately, it's about the simples fix imaginable. All of three characters.