0

I'm trying to measure a filesize in Android using std::ifstream. I found https://stackoverflow.com/a/5840160/5884503 and did this:

    std::ifstream f;
    f.open(filePath, std::ios_base::binary | std::ios::ate);
    if (f.is_open()) {
        ALOGV(TAG, "opened");
    } else {
        ALOGV(TAG, "problem opening");
    }
    auto size = f.tellg();
    f.close();
    __android_log_print(ANDROID_LOG_VERBOSE, "my tag", "filesize: %ld", size);

And I'm getting the filesize as 0 always, even though the file for sure exists and has more than 1mb.

I know this can be done with std::filesystem but this is not already or badly supported on Android. So, how to do with std::ifstream?

Could there be a problem with my formatting? I don't think so.

Gatonito
  • 522
  • 1
  • 9
  • 26
  • Is this answer relevant (tellg reporting incorrect values)? https://stackoverflow.com/a/22986486/783510 – Philipp Claßen Feb 02 '21 at 17:29
  • 1
    I don't know about Android but the file position reported by `tellg` is not guaranteed to begin at `0`. The difference between 'tellg` at the end and the beginning of the file can be used. Maybe that would work for you? https://stackoverflow.com/questions/51352863/what-is-the-idiomatic-c17-standard-approach-to-reading-binary-files/51353040#51353040 – Galik Feb 02 '21 at 17:32
  • >std::filesystem is not already or badly supported on Android - how does it behave? – Nikolay Khilyuk Feb 02 '21 at 21:24

0 Answers0