1

I have some code intending to get the file size of a PNG image (from a different stack overflow post).

#include <fstream>
#include <cstring>
#include <cstddef>
#include <filesystem>

const char* INPUT_FILENAME = "test.png";

using namespace std;

int main()
{
    std::ifstream file;
    size_t size = 0;

    std::cout << "Attempting to open " << INPUT_FILENAME << std::endl;

    file.open( INPUT_FILENAME, std::ios::in | std::ios::ate );
    char* data = 0;

    file.seekg( 0, std::ios::end );
//    size = file.tellg();
    size = std::filesystem::file_size(INPUT_FILENAME);
    std::cout << "File size: " << size << std::endl;
    file.seekg( 0, std::ios::beg );

    data = new char[ size - 8 + 1 ];
    file.seekg( 8 ); // skip the header
    file.read( data, size );
    data[ size ] = '\0';
    std::cout << "Data size: " << std::strlen( data ) << std::endl;
}

Upon running I get the error `no member named 'filesystem' in namespace 'std'; did you mean 'std::__fs::filesystem'. Even attempting to use the latter function results in errors of its own. I need help figuring if this is an issue with not having all the correct packages installed, or possibly my IDE settings? I should note I am running on a MacBook OS 11.2.3 and QtCreator 5.14.2.

LaBeaux
  • 80
  • 1
  • 7
  • 3
    What compiler are you using (clang?), and what version? [`std::filesystem`](https://en.cppreference.com/w/cpp/filesystem) was only introduced in C++17. If your compiler doesn't have full C++17 support, it may not implement `std::filesystem`, or it may only offer a provisional implementation as [`std::experimental::filesystem`](https://en.cppreference.com/w/cpp/header/experimental/filesystem). – Brian61354270 Jun 06 '21 at 00:12

1 Answers1

2

The error is saying that your compiler doesn't support std::filesystem.

There IS such a thing as "std::filesystem" ... depending on your compiler.

This link might help:

https://stackoverflow.com/a/49192230/421195

CONFIG += c++17 can be used with Qt 5.12 and later.

For Qt 5.11 and earlier, it is not a recognized QMake flag and you have to get your hands a bit dirty.

Adding QMAKE_CXXFLAGS += -std=c++17 does the job for GCC & Clang; for MSVC you will probably need to specify /std:c++17 or /std:c++latest

See also Compile With C++17 Mac

If neither option works, please post back with your specific compiler version.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Thank you! This solution worked for me! I have run into the next (possibly related) issue in the suspect line of code `size = std::filesystem::file_size(INPUT_FILENAME);`. I know longer get the filesystem member error, but I receive `file_size' is unavailable: introduced in macOS 10.15`. I am a bit confused as I am running 11.2.3. But if this is too unrelated I will consider submitting under a different thread. – LaBeaux Jun 06 '21 at 03:25
  • 1
    You can always go Old School and simply call [stat()](https://linux.die.net/man/2/stat), [fstat()](https://linux.die.net/man/2/fstat) or friends ;) – paulsm4 Jun 06 '21 at 04:17