1

I have got this code:

#include <fstream>
#include <vector>

int main() {
    const std::string path = "/tmp/some_random_path"; // this file do not exist before running of program
    std::ofstream file_output(path);
    std::ifstream file_input(path, std::ios::in | std::ios::binary);
    file_input.peek();

    if (!file_input.eof()) {
        return 1;
    }
    std::vector<char> data = {'a', 'b', 'c'};
    file_output.write(data.data(), static_cast<long>(data.size()));
    file_output.flush();

    file_input.clear();
    file_input.peek();
    if (file_input.eof()) {
        return 2; // libc++
    }
    return 0; // libstdc++11
}

My specs:

lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.4 LTS Release: 20.04 Codename: focal

clang++ -v Ubuntu clang version 15.0.7 Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10 Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/9 Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10 Candidate multilib: .;@m64 Selected multilib: .;@m64

libstdc++11 compile command: clang++ main.cpp

libc++ compile command: clang++ -stdlib=libc++ main.cpp

I expect success return code for both libc++ and libstdc++11, but libc++ eof flag is not updated. Is it valid?

Tony
  • 33
  • 5
  • Does this answer your question? [.eof() loop not working](https://stackoverflow.com/questions/8734116/eof-loop-not-working) – Pepijn Kramer Feb 02 '23 at 18:21
  • Sounds like clang issue. I've tried `g++` `11.2.0-1.3` with 4 options: default, c++11, c++17 and c++20. In all four cases got return code 0. – Dmitry Messerman Feb 02 '23 at 18:24
  • @PepijnKramer no, this do not answer my question. Or, if it's possible, could you describe, why this link could answer my question? – Tony Feb 03 '23 at 10:32
  • @DmitryMesserman this is definitely not a clang issue, because both program are the same except STL. This is, possible, libc++ issue or, may be, this behaviour is implementation defined. – Tony Feb 03 '23 at 10:34
  • It was an attempt at answering your question, at least I almost never fstream's peek, seek, eof or flush. So at least your code can be simplified, and then you can check if the problem still persists. – Pepijn Kramer Feb 03 '23 at 10:35
  • And I do not know why there is a difference, only that I would write your code like this : https://onlinegdb.com/pV0lWM2MF – Pepijn Kramer Feb 03 '23 at 10:42

0 Answers0